Implicitly typed arrays in C# 3.0

After my C# 3.0 session at the Vic .NET Hands on day, there were several requests to post my examples in greater detail. So, I thought I’d continue the posts I started earlier on the same topic. The first 3 topic were –

Over a series of several posts, I plan to cover the remaining topics –

  • Implicitly Typed Arrays
  • Anonymous Types
  • Auto Implemented Properties
  • Extension Methods
  • Lambda Expression
  • LINQ
  • Expression Trees
  • Partial Methods

In this post, I plan to talk about Implicitly typed arrays. In C# 3.0, as with Implicitly typed local variables, you can also create an array of objects that use type inference to determine what array type they are. So the following statements would create an array of ints and doubles respectively, based on the types of the elements specified within the curly brackets –



//Examples of type inference
var myIntegers = new[] { 1, 2, 3, 4, 5 };
var myDoubles = new[] { 1, 2.1, 3.2, 4.3 }; 

Although the examples show primitive types as array elements, they are not limited to value types. The compiler will automatically infer the type based on the elements and assign it to the correct array type on the left hand side. The example below shows elements using a class Dog and its equivalent in C# 2.0

var myPets = new[] 
    { 
      new Dog( "Spike" ), 
      new Dog( "Snoopy" ) 
    };
 
Dog[] pets = new Dog[2];
pets[0] = new Dog("Spike");
pets[0] = new Dog("Snoopy"); 

What you cannot do

For starters, you cannot mix and match types within your array. You also cannot have nulls when you have an array of value types. So, both the statements shown below are not legal –

//Cannot mix types
var mixedArray = new[] { 0, "one", 2, "three" }; 
//Cannot have nulls, when you mix with value types
var arrayWithNulls = new [] { 1, 2, null };

However, the following statement is acceptable, as it forces the compiler to create an array of nullable ints –

var arrayWithNulls = new[] { (int?)1, 2, 3, null };

Inference when using types from the same inheritance tree

When you have elements in the array that have types from the same inference tree, at least one of the types in the list have will have to be base class. For example, if we have the inheritance as shown below –

image

then, the first statement will not work, while the second one will –


var myPets = new[] { new Cat(), new Dog() } ;
var myPets1 = new[] { new Cat(), new Pet() } ;

If you want to create an array of IAnimal elements, then at least one of the elements in the array will have to be explicitly typed to an IAnimal as shown below –



IAnimal dog = new Dog();
var myPets2 = new[] { dog, new Cat() };

You cannot call it Implicitly typed, then. Can you? 🙂

Advertisements

One response

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s