Implicitly Typed Arrays (C# Programming Guide)


Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

You can create an implicitly-typed array in which the type of the array instance is inferred from the elements specified in the array initializer. The rules for any implicitly-typed variable also apply to implicitly-typed arrays. For more information, see Implicitly Typed Local Variables.

Implicitly-typed arrays are usually used in query expressions together with anonymous types and object and collection initializers.

The following examples show how to create an implicitly-typed array:

            class ImplicitlyTypedArraySample
                static void Main()
                    var a = new[] { 1, 10, 100, 1000 }; // int[]
                    var b = new[] { "hello", null, "world" }; // string[]

                    // single-dimension jagged array
                    var c = new[]   

                    // jagged array of strings
                    var d = new[]   
                new[]{"Luca", "Mads", "Luke", "Dinesh"},
                new[]{"Karen", "Suma", "Frances"}

In the previous example, notice that with implicitly-typed arrays, no square brackets are used on the left side of the initialization statement. Note also that jagged arrays are initialized by using new [] just like single-dimension arrays.

When you create an anonymous type that contains an array, the array must be implicitly typed in the type's object initializer. In the following example, contacts is an implicitly-typed array of anonymous types, each of which contains an array named PhoneNumbers. Note that the var keyword is not used inside the object initializers.

                    var contacts = new[] 
                new {
                        Name = " Eugene Zabokritski",
                        PhoneNumbers = new[] { "206-555-0108", "425-555-0001" }
                new {
                        Name = " Hanying Feng",
                        PhoneNumbers = new[] { "650-555-0199" }

C# Programming Guide
Implicitly Typed Local Variables
Anonymous Types
Object and Collection Initializers
LINQ Query Expressions