New Keyword Interview Question

22 Sep 2021

Part of the duties of a senior team member is to interview candidates for open positions. This task comes up for various reasons: attrition, growth, and new projects. Interviews are often done for other teams as well. Let's face it, interviews are part of the job.

When interviewing developers I give them the opportunity to self rank (from 1..10) on their understanding of a language or technology. I will ask something along the lines of:

If 1 is a complete beginner and 10 means you're an expert in X, where would you rank your understanding between 1 and 10?

Yes, this is a loaded question. However, I like it because it allows the candidate to consider where they stand, and more importantly, what they want to share. A rank of 10 would seem arrogant. Even if you were an expert I would guess that you should not say 10. The goal is to be honest with yourself first. There are always things you do not understand. You can see the calculation going as most candidates will say ... 7.

If you are a C# developer, and you say 8+ you will then get this question: "How many uses of the keyword new exist in the C# language?" In the C# programming language the new keyword can be used as:

Most developers use and will provide examples of using the operator to instantiate objects and arrays.

The new operator used to invoke constructors, arrays, and instantiating anonymous types. This is the most common usage and documented here.

// object
var str = new StringBuilder();
str.Append("new operator");

// array
var newTypes = new int[3];
newTypes[0] = 1;
newTypes[1] = 2;
newTypes[2] = 3;

// anonymous
var a = new { Text = "anonymous object" };
Console.WriteLine($"{a.Text}");

Someone who has programmed C# professionally, for any length of time, will have used generics. If they wrote generic classes they may have encountered the constraint usage. This is good indicator they are writing libraries or shared code for their projects. This constraint is used when the generic class is required to instantiate a type. I have used this to create domain objects and then fill them up with data programmatically, generally with reflection.

The constraint where T : new() ensures the type has a default (parameterless) constructor and is documented here.

// constraint
public class Factory<T> where T : new()
{
  public T Create()
  {
    var o = new T();
    // initialize 'o' with common params
    return o;
  }
}

Experience has shown that the new constraint usage is usually associated with creating and setting up some object or properties. The generic type will also have an interface implemented that can be used to properly initialize the newly created object.

The final usage for the keyword new is as a modifier. This modifier is used to when a method in a class conflicts or hides the base class method of the same name.

I have only used the modifier when integrating into an existing library or shared code, and I absolutely refuse, or cannot, change the name of the class member. If you are okay with changing the derived member name, even just adding a character, then this modifier is generally not required.

The modifier usage is documented here.

// modifier
public class BaseClass
{
    public string Name { get; } = "Base";
}

public class DerivedClass : BaseClass
{
    public new string Name { get; } = "Derived";
    // just change the name b/c they really are 2 different members
    public string Name2 { get; } = "Derived";
}

Is Name2 a properly named member? Depending upon your review standards ... well ... NO! In the real world if you find yourself using that you may just want to consider using the new modified.

< back