在 C# 中使用 new 关键字
csharpprogrammingserver side programming更新于 2025/9/24 13:37:17
使用 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() 方法
arrSource.CopyTo(arrTarget,0 );
Console.WriteLine("Destination Array ...");
foreach (int value in arrTarget) {
Console.WriteLine(value);
}
}
}
输出
Destination Array ... 5 9 1 3

