在C#中使用new关键字

使用new关键字创建数组的实例。new运算符用于创建对象或实例化对象。在此示例中,使用新对象为类创建了一个对象。

以下是一个示例。

Calculate c = new Calculate();

您还可以使用new关键字创建数组的实例。

double[] points = new double[10];

new关键字还用于创建集合的对象。

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

让我们来看一个例子。

示例

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

输出结果

Destination Array ...
5
9
1
3