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
Using the new keyword in C#
The new keyword in C# is used to create instances of objects, arrays, and collections. It allocates memory for the object and calls the appropriate constructor to initialize it.
Syntax
Following is the syntax for using the new keyword to create objects −
ClassName objectName = new ClassName();
Following is the syntax for creating arrays using new −
dataType[] arrayName = new dataType[size];
Following is the syntax for creating collections using new −
CollectionType<T> collectionName = new CollectionType<T>();
Using 'new' to Create Objects
The most common use of the new keyword is to create instances of classes −
using System;
class Calculate {
public int Add(int a, int b) {
return a + b;
}
public void Display() {
Console.WriteLine("Calculate object created successfully");
}
}
class Program {
static void Main() {
Calculate c = new Calculate();
c.Display();
int result = c.Add(10, 20);
Console.WriteLine("Addition result: " + result);
}
}
The output of the above code is −
Calculate object created successfully Addition result: 30
Using 'new' to Create Arrays
You can use the new keyword to create arrays of any data type −
using System;
class Program {
static void Main() {
// Creating different types of arrays
int[] numbers = new int[5];
double[] points = new double[3];
string[] names = new string[2];
// Initialize array elements
numbers[0] = 10;
numbers[1] = 20;
points[0] = 3.14;
names[0] = "Alice";
Console.WriteLine("Integer array first element: " + numbers[0]);
Console.WriteLine("Double array first element: " + points[0]);
Console.WriteLine("String array first element: " + names[0]);
}
}
The output of the above code is −
Integer array first element: 10 Double array first element: 3.14 String array first element: Alice
Using 'new' to Create Collections
The new keyword is also used to instantiate collection objects like List, SortedList, and others −
using System;
using System.Collections;
using System.Collections.Generic;
class Program {
static void Main() {
// Creating a SortedList
SortedList sl = new SortedList();
sl.Add(1, "First");
sl.Add(3, "Third");
sl.Add(2, "Second");
// Creating a List of strings
List<string> myList = new List<string>();
myList.Add("Apple");
myList.Add("Banana");
myList.Add("Cherry");
Console.WriteLine("SortedList contents:");
foreach (DictionaryEntry item in sl) {
Console.WriteLine(item.Key + ": " + item.Value);
}
Console.WriteLine("\nList contents:");
foreach (string fruit in myList) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
SortedList contents: 1: First 2: Second 3: Third List contents: Apple Banana Cherry
Array Copy Example
Here's an example demonstrating array creation and copying using the new keyword −
using System;
class Program {
static void Main() {
int[] arrSource = new int[4];
arrSource[0] = 5;
arrSource[1] = 9;
arrSource[2] = 1;
arrSource[3] = 3;
int[] arrTarget = new int[4];
// CopyTo() method
arrSource.CopyTo(arrTarget, 0);
Console.WriteLine("Destination Array ...");
foreach (int value in arrTarget) {
Console.WriteLine(value);
}
}
}
The output of the above code is −
Destination Array ... 5 9 1 3
Conclusion
The new keyword in C# is essential for creating instances of objects, arrays, and collections. It allocates memory and initializes the created instance, making it ready for use in your application.
