Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the use of 'new' keyword in C#?
The new keyword in C# serves multiple important purposes. It is primarily used to create new instances of classes, allocate memory for arrays, and hide inherited members from base classes.
Syntax
Following is the syntax for creating object instances using new −
ClassName objectName = new ClassName();
Following is the syntax for creating arrays using new −
dataType[] arrayName = new dataType[size];
Following is the syntax for hiding base class members using new −
public new void MethodName() {
// hides the base class method
}
Using 'new' to Create Object Instances
The most common use of the new keyword is to instantiate objects from classes −
using System;
class Student {
public string name;
public int age;
public Student(string name, int age) {
this.name = name;
this.age = age;
}
public void DisplayInfo() {
Console.WriteLine("Name: " + name + ", Age: " + age);
}
}
class Program {
static void Main(string[] args) {
Student student1 = new Student("Alice", 20);
Student student2 = new Student("Bob", 22);
student1.DisplayInfo();
student2.DisplayInfo();
}
}
The output of the above code is −
Name: Alice, Age: 20 Name: Bob, Age: 22
Using 'new' to Create Arrays
The new keyword is essential for creating arrays and allocating memory −
using System;
class Program {
static void Main(string[] args) {
int[] numbers = new int[5] {10, 20, 30, 40, 50};
string[] names = new string[3];
names[0] = "John";
names[1] = "Jane";
names[2] = "Mike";
Console.WriteLine("Numbers array:");
for(int i = 0; i < numbers.Length; i++) {
Console.WriteLine("numbers[" + i + "] = " + numbers[i]);
}
Console.WriteLine("\nNames array:");
for(int i = 0; i < names.Length; i++) {
Console.WriteLine("names[" + i + "] = " + names[i]);
}
}
}
The output of the above code is −
Numbers array: numbers[0] = 10 numbers[1] = 20 numbers[2] = 30 numbers[3] = 40 numbers[4] = 50 Names array: names[0] = John names[1] = Jane names[2] = Mike
Using 'new' to Hide Base Class Members
The new keyword can hide methods or properties from a base class, creating a completely new implementation −
using System;
class BaseClass {
public void Display() {
Console.WriteLine("Display method in BaseClass");
}
}
class DerivedClass : BaseClass {
public new void Display() {
Console.WriteLine("Display method in DerivedClass");
}
}
class Program {
static void Main(string[] args) {
BaseClass baseObj = new BaseClass();
DerivedClass derivedObj = new DerivedClass();
BaseClass baseRef = new DerivedClass();
baseObj.Display();
derivedObj.Display();
baseRef.Display();
}
}
The output of the above code is −
Display method in BaseClass Display method in DerivedClass Display method in BaseClass
Comparison: 'new' vs 'override'
| new Keyword | override Keyword |
|---|---|
| Hides the base class member completely | Overrides a virtual/abstract method |
| Calls base method when accessed through base reference | Calls derived method even through base reference |
| Does not require base method to be virtual | Requires base method to be virtual or abstract |
Conclusion
The new keyword in C# is versatile, serving to instantiate objects, allocate memory for arrays, and hide base class members. Understanding its different uses is essential for effective C# programming and object-oriented design patterns.
