Rectangle 27 0

All possible C array initialization syntaxes?


int[,,,]
int[,,]
int[] x = { 10, 20, 30 };
new int[,] { { 3, 7 }, { 103, 107 }, { 10003, 10007 }, };
new int[3]
new int[3] { 10, 20, 30 }
new int[] { 10, 20, 30 }
new[] { 10, 20, 30 }

@BoltClock: The first syntax you mention is an "implicitly typed array creation expression". The second is an "anonymous object creation expression". You do not list the other two similar syntaxes; they are "object initializer" and "collection initializer".

@Jeffrey: If we're going down that road,it starts getting silly. E.g., "1,2,3,4".split(',').

In the first one, the size may be any non-negative integral value and the array elements are initialized to the default values.

In the fourth one the type of the array element is inferred by computing the best type, if there is one, of all the given elements that have types. All the elements must be implicitly convertible to that type. The size is determined from the number of elements given. This syntax was introduced in C# 3.0.

In the second one, the size must be a constant and the number of elements given must match. There must be an implicit conversion from the given elements to the given array element type.

In the third one, the elements must be implicitly convertible to the element type, and the size is determined from the number of elements given.

Not exactly C# "syntax", but let's not forget (my personal favorite) Array.CreateInstance(typeof(int), 3)!

The array creation syntaxes in C# that are expressions are:

The elements must be implicitly convertible to the element type. The size is determined from the number of elements given.

There is also a syntax which may only be used in a declaration:

Why do some of these require implicit convertability of the array elements to the array type, while others require explicit convertability?

there isn't an all-in-one guide

Note
Rectangle 27 0

All possible C array initialization syntaxes?


Enumerable.Repeat(String.Empty, count).ToArray()
var arr1 = Enumerable.Repeat(new object(), 10).ToArray();
var arr2 = Enumerable.Repeat(/* dummy: */ false, 10).Select(x => new object()).ToArray();

Will create array of empty strings repeated 'count' times. In case you want to initialize array with same yet special default element value. Careful with reference types, all elements will refer same object.

Note
Rectangle 27 0

All possible C array initialization syntaxes?


private DummyUser[] arrDummyUser = new DummyUser[]
{
    new DummyUser{
       email = "abc.xyz@email.com",
       language = "English"
    },
    new DummyUser{
       email = "def@email.com",
       language = "Spanish"
    }
};
public class DummyUser
{
    public string email { get; set; }
    public string language { get; set; }
}

Below is the class definition.

This is how you can initialize the array:

Note
Rectangle 27 0

All possible C array initialization syntaxes?


string[] array = new string[2]; // creates array of length 2, default values
string[] array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2
var array = new string[2]; // creates array of length 2, default values
var array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2

@Joshua, I suggest moving the acceptance tick over to Eric Lippert's answer. His is much more complete and will serve a greater benefit to those with similar questions.

Also note that in the declarations above, the first two could replace the string[] on the left with var (C# 3+), as the information on the right is enough to infer the proper type. The third line must be written as displayed, as array initialization syntax alone is not enough to satisfy the compiler's demands. So if you're into the whole brevity thing, the above could be written as

Note that other techniques of obtaining arrays exist, such as the Linq ToArray() extensions on IEnumerable<T>.

These are the current declaration and initialization methods for a simple array.

Note
Rectangle 27 0

All possible C array initialization syntaxes?


It would be good to more clearly separate the invalid syntaxes from the valid ones.

Only expressions that can be assigned with the var keyword can be passed as arguments.

data9 which is more descriptive is not compilable but data11 that is less descriptive is compilable. This fact looks strange, right?

Note
Rectangle 27 0

All possible C array initialization syntaxes?


int[,,,]
int[,,]
int[] x = { 10, 20, 30 };
new int[,] { { 3, 7 }, { 103, 107 }, { 10003, 10007 }, };
new int[3]
new int[3] { 10, 20, 30 }
new int[] { 10, 20, 30 }
new[] { 10, 20, 30 }

@BoltClock: The first syntax you mention is an "implicitly typed array creation expression". The second is an "anonymous object creation expression". You do not list the other two similar syntaxes; they are "object initializer" and "collection initializer".

@Jeffrey: If we're going down that road,it starts getting silly. E.g., "1,2,3,4".split(',').

In the first one, the size may be any non-negative integral value and the array elements are initialized to the default values.

In the fourth one the type of the array element is inferred by computing the best type, if there is one, of all the given elements that have types. All the elements must be implicitly convertible to that type. The size is determined from the number of elements given. This syntax was introduced in C# 3.0.

In the second one, the size must be a constant and the number of elements given must match. There must be an implicit conversion from the given elements to the given array element type.

In the third one, the elements must be implicitly convertible to the element type, and the size is determined from the number of elements given.

Not exactly C# "syntax", but let's not forget (my personal favorite) Array.CreateInstance(typeof(int), 3)!

The array creation syntaxes in C# that are expressions are:

The elements must be implicitly convertible to the element type. The size is determined from the number of elements given.

There is also a syntax which may only be used in a declaration:

there isn't an all-in-one guide

Note
Rectangle 27 0

All possible C array initialization syntaxes?


var array = Enumerable.Repeat(string.Empty, 37).ToArray();

Also please take part in this discussion.

In case you want to initialize a fixed array of pre-initialized equal (non-null or other than default) elements, use this:

Note
Rectangle 27 0

All possible C array initialization syntaxes?


string[] array = new string[2]; // creates array of length 2, default values
string[] array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2
var array = new string[2]; // creates array of length 2, default values
var array = new string[] { "A", "B" }; // creates populated array of length 2
string[] array = { "A" , "B" }; // creates populated array of length 2

@Joshua, I suggest moving the acceptance tick over to Eric Lippert's answer. His is much more complete and will serve a greater benefit to those with similar questions.

Also note that in the declarations above, the first two could replace the string[] on the left with var (C# 3+), as the information on the right is enough to infer the proper type. The third line must be written as displayed, as array initialization syntax alone is not enough to satisfy the compiler's demands. So if you're into the whole brevity thing, the above could be written as

Note that other techniques of obtaining arrays exist, such as the Linq ToArray() extensions on IEnumerable<T>.

These are the current declaration and initialization methods for a simple array.

Note
Rectangle 27 0

All possible C array initialization syntaxes?


Dim i, j As Integer
    Dim strArr(1, 2) As String

    strArr(0, 0) = "First (0,0)"
    strArr(0, 1) = "Second (0,1)"

    strArr(1, 0) = "Third (1,0)"
    strArr(1, 1) = "Fourth (1,1)"
int[] array = new int[4]; 
array[0] = 10;
array[1] = 20;
array[2] = 30;
string[] array = { "Sunday" , "Monday" };
string[] week = new string[] {"Sunday","Monday","Tuesday"};

Hi, the last block of examples appear to be Visual Basic, the question asks for c# examples.

and in multi dimensional array

Note