Using the new keyword in C#

CsharpProgrammingServer Side Programming

Use the new keyword to create an instance of the array. The new operator is used to create an object or instantiate an object. Here in the example an object is created for the class using the new.

The following is an example.

Calculate c = new Calculate();

You can also use the new keyword to create an instance of the array.

double[] points = new double[10];

The new keyword is also used to create object of a collection.

SortedList sl = new SortedList(); // SortedList
List<string> myList = new List<string>() // List

Let us see an example.

Example

 Live Demo

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);
      }
   }
}

Output

Destination Array ...
5
9
1
3
raja
Published on 16-Aug-2018 12:57:45
Advertisements