new Keyword in C#

Posted: July 29, 2012 in .NET, C#
Tags:

The “new” keyword in C# is one of the most commonly known and used keywords in C#.It is used as an operator for instantiating objects of a class as shown below.

List<int> l = new List<int>();

But there are two other uses of the new keyword as well. The first one to be discussed is new as an modifier.But first let’s take a look at method hiding in C#. Refer to the following piece of code.

class Program 
    { 
        static void Main(string[] args) 
        { 
            A a = new A(); 
            a.M1(); 
            B b = new B(); 
            b.M1(); 
            a = new B(); 
            a.M1(); 
            Console.Read(); 
        } 
    }

public class A 
    { 
        int i = 10; 
        public void M1() 
        { 
            Console.WriteLine("A::M1"); 
        } 
    } 
    public class B : A 
    { 
        public void M1() 
        { 
            Console.WriteLine("B::M1"); 
        } 
    }

Here the base class A has a method public void M1() and the derived class provides a another new implementation of the same method i.e it is hiding the implementation of the base class.

The output of this program will be :

A::M1
B::M1
A::M1

So it is very clear that the method invoked depends upon the compile time type of the object variable rather than runtime type of the object being instantiated( which is the case for overriding).

But the above code also throws a compile time warning:

CS0108: ‘NewKeyWordDemo.B.M1()’ hides inherited member ‘NewKeyWordDemo.A.M1()’. Use the new keyword if hiding was intended.

So we can use the new keyword as a modifier to clearly indicate the method hiding as shown below:

public new void M1() 
{ 
     Console.WriteLine("B::M1"); 
} 

Note: Method hiding works without the new keyword as well but it’s a good practice

Another use of the new keyword is as a constraint for generic types. Refer to the following code:

public class G<T> where T : new()
{
    public void M1(T t)
    {
        Console.WriteLine(t.GetType().FullName);
    }
}

The constraint T:new() indicates that

  • Type T must have a parameterless constructor
  • Type T must be instantiable i.e not an abstract class.

The following code will fail with a compilation error.

public class A
    {
        int i = 10;
        public A(int i)
        {
            this.i = i;
        }
        public void M1()
        {
            Console.WriteLine("A::M1");
        }
    }

    public class G<T> where T : new()
    {
        public void M1(T t)
        {
            Console.WriteLine(t.GetType().FullName);
        }
    }

Now class A has a parameterized constructor so compilation will fail with the following error:

CS0310: ‘NewKeyWordDemo.A’ must be a non-abstract type with a public parameterless constructor in order to use it as parameter ‘T’ in the generic type or method ‘NewKeyWordDemo.G<T>’

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.