C# new vs override

Muhammad Maisam Abbas Feb 16, 2024
  1. the new Keyword in C#
  2. the override Keyword in C#
C# new vs override

This tutorial will discuss the difference between new and override keywords in C#.

the new Keyword in C#

The new keyword is very common in C#. Traditionally, it can be used in three distinct places: operator, modifier, and constraint.

The first and the most popular use of the new keyword is as an operator to create class objects and invoke their class constructors in C#. With the new keyword, value-type objects like int and float are created inside the stack, and reference-type objects like user-defined classes are created inside the heap.

Some things to keep in mind while utilizing the new keyword as an operator are: it only assigns the memory to the object and doesn’t destroy any memory that previously depends upon the same scope, and we cannot override the functionality of the new keyword in C#.

Secondly, while using the new keyword as a modifier, it hides the base class members inherited from the derived class. The previous statement means that instead of overriding or declaring a new definition of that inherited class member, the new keyword replaces the previous definition of that member in the base class.

The third use of the new keyword is relatively uncommon compared to the previous two. While using the new keyword as a constraint, we specify that the generic type must have a public constructor without additional parameters.

The disadvantage of this approach is that we can’t use it with an abstract type.

For the sake of this tutorial, we’ll use the new keyword as a modifier to hide the definition of the base class from the derived class. In C#, the compiler implicitly hides the definition of any inherited functions by default.

The following code snippet demonstrates how we can explicitly hide the definition of an inherited function from the derived class using the new keyword.

public class BaseClass {
  public virtual void fun() {
    Console.WriteLine("Base class");
  }
}

public class DerivedClass : BaseClass {
  public new void fun() { Console.WriteLine("Derived class");
}
public static void Main(string[] args) {
  BaseClass b = new BaseClass();
  DerivedClass d = new DerivedClass();
  BaseClass bd = new DerivedClass();
  b.fun();
  d.fun();
  bd.fun();
}
}

Output:

Base class
Derived class
Base class

The output for the first two objects is pretty straightforward; the BaseClass object b references the fun() function inside the base class and the DerivedClass object d references the fun() function inside the derived class.

A very interesting thing happens when we assign the reference of the derived class to an object of the base class bd and invoke the fun() function. The fun() function inside the base class is still executed this time.

This is because implementing the fun() function inside the base class is hidden from the derived class.

This same objective can be achieved without using the new keyword. The following code snippet shows how we can achieve the same objective without using the new keyword.

public class BaseClass {
  public virtual void fun() {
    Console.WriteLine("Base class");
  }
}

public class DerivedClass : BaseClass {
  public void fun() {
    Console.WriteLine("Derived class");
  }
  public static void Main(string[] args) {
    BaseClass b = new BaseClass();
    DerivedClass d = new DerivedClass();
    BaseClass bd = new DerivedClass();
    b.fun();
    d.fun();
    bd.fun();
  }
}

Output:

Base class
Derived class
Base class

Notice that the output is the same as the previous approach. The only difference here is that this time, we get a compiler warning saying that to explicitly hide the class member, use the new keyword.

compiler warning without new keyword

the override Keyword in C#

In C#, the override modifier extends the inherited method from the base class. The override keyword provides a new implementation of the virtual method inherited from the base class.

One thing to note here is that we cannot use the override keyword to extend a non-virtual or static method. The function in the base class must be virtual, abstract, or overridden by another super class.

In the context of our previous example, the following code snippet shows how we can use the override modifier to extend the functionality of a virtual function inherited from the base class.

public class BaseClass {
  public virtual void fun() {
    Console.WriteLine("Base class");
  }
}

public class DerivedClass : BaseClass {
  public override void fun() {
    Console.WriteLine("Derived class");
  }
  public static void Main(string[] args) {
    BaseClass b = new BaseClass();
    DerivedClass d = new DerivedClass();
    BaseClass bd = new DerivedClass();
    b.fun();
    d.fun();
    bd.fun();
  }
}

Output:

Base class
Derived class
Derived class

The output for the first two objects is pretty straightforward; the BaseClass object b references the fun() function inside the base class and the DerivedClass object d references the fun() function inside the derived class.

The real difference from the new keyword appears when we assign the reference of the derived class to an object of the base class bd and invoke the fun() function. Unlike the previous two examples, this time, the fun() function of the derived class gets invoked.

The override keyword is used to show the child implementation of a virtual function to its parent object, whereas the new keyword is used to hide the child implementation from its parent class object.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp Keyword