Implicitly Typed Arrays

C# 3.0 introduced an abbreviated way of initializing arrays when the type of the array can be inferred at runtime. Let’s have a look at the new syntax in the following code snippet:

using System;
public class EntryPoint
static void Main() {
// A conventional array
int[] conventionalArray = new int[] { 1, 2, 3 };
// An implicitly typed array
var implicitlyTypedArray = new [] { 4, 5, 6 };
Console.WriteLine( implicitlyTypedArray.GetType() );
// An array of doubles
var someNumbers = new [] { 3.1415, 1, 6 };
Console.WriteLine( someNumbers.GetType() );
// Won't compile!
// var someStrings = new [] { "int",
// someNumbers.GetType() };

For comparison purposes, the first array variable, named conventionalArray, uses one of the conventional syntaxes for declaring and instantiating an array. However, the next variable, implicitlyTypedArray, uses the new and shorter syntax, and so it is devoid of any type information. Instead of providing the compiler with type information, I instead rely on the compiler to deduce that each item in the array is of type int. And to save even more keystrokes and make my life a bit easier, implicitlyTypedArray is declared as an implicitly typed local variable. If you execute this code, you will see that the WriteLine method call right below it shows that the implicitly typed variable is of type System.Int32[]. In fact, you could have expressed the same line of code as follows:

int[] implicitlyTypedArray = new [] { 4, 5, 6 };

However, because you’ve already got the compiler figuring out the type of the array elements, you may as well go a little further and let it deduce the entire array type, especially if the variable remains local to the scope of this method. But what happens if you declare the array using multiple types in the initialization list?

When the compiler is presented with multiple types within the initialization list of an implicitly typed array, it determines a type that all the given types are implicitly convertible to. And, of course, for all practical purposes, any type instance is convertible to its own type. Therefore, in the declaration of the someNumbers instance in the example, the compiler determines that all of the types within the braces are convertible to System.Double. Not surprisingly, the following WriteLine method call confirms this. But what if the types are not all implicitly convertible to a single type?

When the compiler cannot find a common type that all of the array variables are convertible to, it will present the compiler warning CS0826, stating

No best type found for implicitly typed array

If you uncomment the line where I have declared the someStrings variable, you will see this behavior, because System.Type instances are not implicitly convertible to System.String.

But what happens if you declare an array with two items and both types are convertible to each other? Getting into a situation like this is very rare and usually has to be induced by having two custom types that both contain implicit conversion operators to each other’s type.1 Just to see what happened, I did just that. When I attempted to declare an implicitly typed array with an item of each type, I was greeted with the CS0826 compiler error again, which I expected.

You may already be thinking about many useful applications for implicitly typed arrays. But in most cases, they merely save you some typing.2 This is handy if your array contains closed generic types that require a lot of typing to specify their type. So, in that respect, implicitly typed arrays can make more readable code. But in other cases, they can actually make code harder to follow for a maintenance engineer if knowing the type of the array at the point of declaration is essential to easily reading and understanding what the code is doing.

All objects in .NET are implicitly convertible to System.Object; therefore, you may be wondering why the compiler won't settle on using System.Object as the common type in the previous assignment to someStrings. That is because the compiler may only use types that are present in the implicitly typed array expression. Since System.Object is not one of the types listed in the assignment of someStrings, it is not considered.

Implicitly typed variables are actually implicitly typed local variables, so unless the method you are reading is overly complex and huge, you should have no trouble deducing the type yourself. If you have trouble deducing the type from looking at the code, it may mean that the function is too complex and in need of some refactoring. Additionally, if you’re using implicitly typed local variables in your code, be sure the name the variables logically so that a maintenance engineer can deduce the type of the variable easily.

All of that said, implicitly typed arrays are great for instantiating n-tuples of items. For example, the following code snippet shows a succinct way to declare a matrix of integers:

using System;

public class EntryPoint {
static void Main() {
var threeByThree = new [] {
new [] { 1, 2, 3 },
new [] { 4, 5, 6 },
new [] { 7, 8, 9 }
foreach( var i in threeByThree ) {
foreach( var j in i ) {
Console.Write( "{0}, ", j );
Console.Write( "\n" );

Source Of Information : Apress Accelerated C Sharp 2010


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner