C# 3.0 : New Features

April 24, 2008

Implicitly Typed Local Variables

“Var” allows declaring a new variable of type var, whose type is implicitly inferred from the expression used to initialize the variable. Local variables can be declared as type var, Compiler does actually determine the actual type based on the data it is initialized.

It must be initialized in the same statement. The value that the variable is being initialized to determines the type of the variable.

var a = 3; –> a is created of type int

var str = “Priya” –> str is created of type string.

var a; (Gives error)  

 Usage of var  –> Used to store anonymous types as in LINQ based programming.  

var languageQuery = from one in Languages where one == “VC++” select new { one };

The part “new { one }” declares and instantiates an anonymous type.  Additionally the query probably returns a collection of that anonymous type. 

Implicitly typed arrays, on the other hand, are possible using a different syntax, as shown below:     

        var strArr = new[] {C, C++, C#}

The above line of code would end up declaring strArr as string[], we always know the type of the variable by looking at its declaration. They cannot be initialized to null and Cannot be used as Class members.

Anonymous Types

C# 3.0 gives the flexibility to create an instance of a class without having to write code for the class before hand as shown below.

new {nationality=”Indian”, hair =”Brown”, age=24

The above line of code, gives a new type that has three properties: nationality, skin, and age. Internally, the C# compiler would create a class that looks as follows, but it’s not accessible by application code.

class __Anonymous
{
private string _nationality = “Indian”;
private string _skin = ” White”;
private int _age = 24;
public string nationality {get { return _ nationality; } set { _ nationality = value;}}
public string hair {get { return _ hair; } set { _ hair = value; }}
public int age {get { return _ age; } set { _ age = value; }}
}

If another anonymous type, with the same sequence of names and types were created, then the compiler would create only a single anonymous type for both instances to use. Also, because the instances are, as you may have guessed, simply instances of the same class, they can be exchanged because the types are really the same.  

Now that we have a class, but still need something to hold an instance of the above class. This is where the “var” keyword comes in handy; it lets us hold a statically typed instance of the above instance of the anonymous type. Here is a rather simple and easy use of an anonymous type. 

 var priya = new {nationality=”black”, hair=” Brown“, age=24}

 

Anonymous types are of class types which can have only public read-only properties as their members. No other class members like methods are allowed.  

They are of referrence types and are derived from ‘Object’ class. i.e. Anonymous types are automatically inferred and created from object initializers(An object initializer specifies values from one or more properties of an object. Which means one can specify a set of properties of an object through a series of assignments, such as {nationality=”Indian”, hair =”Brown”, age=24}, and an object is assigned these properties)

They have a method scope.

// example.cs

using System;
using System.Linq;
using System.Text;

namespace AnonymousTypes
{
class Program
{
static void Main(string[] args)
{
var priya = new {Nationality = “Indian”, Hair = “Brown”, Age = 24 };
Console.WriteLine(“Nationality = {0}\nHair = {1}\nAge = {2}”, priya. Nationality, priya. Hair, priya.Age);

Console.ReadLine();
}
}
}

Read the rest of this entry »