C#array initialized multiple methods
The most direct method is to use
New keywords, followed by array type and required size:
<code class="language-csharp">string[] array = new string[2]; // 创建长度为 2 的数组,初始化为默认值</code>
<code class="language-csharp">string[] array = new string[] { "A", "B" }; // 创建长度为 2 的数组,初始化为 "A" 和 "B"</code>
New Keywords:
<code class="language-csharp">string[] array = { "A", "B" }; // 创建长度为 2 的数组,初始化为 "A" 和 "B"</code>
VAR to infer the type of array from the initial grammar:
<code class="language-csharp">var array = new[] { "A", "B" }; // 创建长度为 2 的已填充数组,类型从值推断</code>
<code class="language-csharp">string[] array = ["A", "B"]; // 创建长度为 2 的已填充数组,类型从值推断</code>
The above is the detailed content of How Many Ways Can I Initialize an Array in C#?. For more information, please follow other related articles on the PHP Chinese website!