C# 数组初始化语法有哪些?
C# 中的数组可以用多种方法初始化。我们来看一些示例。
方法一
使用数组的大小。
int [] marks = new int[5] { 99, 98, 92, 97, 95};方法二
省略大小。
int [] marks = new int[] { 99, 98, 92, 97, 95};方法三
在声明时初始化。
int [] marks = { 99, 98, 92, 97, 95};我们来看 C# 中数组初始化的一种方法。
示例
using System;
namespace Demo {
class MyArray {
static void Main(string[] args) {
int [] n = new int[10]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100;
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}输出
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP