C#array initialization method Detailed explanation
C# offers a variety of array initialization syntax:
<code class="language-csharp">int[] numbers = new int[5]; </code>
The direct use of value initialization of the array:
<code class="language-csharp">string[] names = new string[] { "John", "Mary", "Bob" };</code>
Create a new array with initialized expressions:
<code class="language-csharp">int[] numbers = { 1, 2, 3, 4, 5 };</code>
Collection expression (new features of C# 12):
<code class="language-csharp">int[] numbers = new[] { 1, 2, 3, 4, 5 };</code>
Supplementary description:
<code class="language-csharp">int[] numbers = [1, 2, 3, 4, 5];</code>
The third grammar needs to be explicitly declared in front of the bracket. The fourth grammar uses
expression, which also supports type inference.var
The above is the detailed content of What are the Different Array Initialization Syntaxes in C#?. For more information, please follow other related articles on the PHP Chinese website!