Local Type inference-Implicitly typed local variables June 28, 2008
Posted by fofo in .NET, C#, Visual Studio 2008.trackback
This post is belongs to a series of posts that explains the new features in C# 3.0.
A new feature that comes with c# 3.0 are implicitly typed local variables. C# 3.0 introduces tha var keyword and it is used to create a variable without having to know the type of that particular variable. A LINQ query may return something that we do not know the type. We can leave it to the C# compiler to decide what type that is.Please note that the var keyword only works within the local scope. We will try to show this new feature with a step by step example.
1) Create an ASP.NET web application in c#
2) In the page load event of the .aspx page place the code below
int mynumber = 67;
Response.Write(“the type of this variable is” + ” ” + “<strong>” + mynumber.GetType()+ “</strong>”);
Response.Write(“<br/>”);
var mynumber1 = 68;
Response.Write(“the type of this variable is” + ” ” + “<strong>” + mynumber1.GetType() + “</strong>”);
Response.Write(“<br/>”);
By using the GetType method i can get the type of the variable (int mynumber=67;). When i run the application i get the message
the type of this variable is System.Int32
Replacing int mynumber=67 with var mynumber1=68 and running the application i get the same message
the type of this variable is System.Int32
It does recognise the type of the variable mynumber1.
3) Type the code below in the page load event.
object mynumber2 = 78;
Response.Write(“the type of this variable is” + ” ” + “<strong>” + mynumber2.GetType() + “</strong>”);
We still get the same output
the type of this variable is System.Int32
But object and var are not the same. if we type this code just below the previous statement we would it expect it to work
// this will not work -comment that out later
mynumber=mynumber2;
mynumber is an int and mynumber2 is an object. We will receive the following error
ECannot implicitly convert type ‘object’ to ‘int’. An explicit conversion exists (are you missing a cast?)
but if we type this code just below the previous bit
mynumber = mynumber1;
we will be okey. mynumber is an int and mynumber1 variable is proceeded with the var keyword(see the previous bits of code)
so var and object are not the same.
All the code that goes inside the page load event is following
int mynumber = 67;
Response.Write(“the type of this variable is” + ” ” + “<strong>” + mynumber.GetType()+ “</strong>”);
Response.Write(“<br/>”);
var mynumber1 = 68;
Response.Write(“the type of this variable is” + ” ” + “<strong>” + mynumber1.GetType() + “</strong>”);
Response.Write(“<br/>”);
object mynumber2 = 78;
Response.Write(“the type of this variable is” + ” ” + “<strong>” + mynumber2.GetType() + “</strong>”);
// this will not work
mynumber=mynumber2;
// this will work
mynumber = mynumber1;
If we go and type this var myint=65; outside the page load event we will get a compile error
The contextual keyword ‘var’ may only appear within a local variable declaration
Also you cannot use var to define an input parameter. see the example below
public void mygreatmethod(var mystring)
{
}
If you try to run the application with this snippet of code added you will receive the same error as before.
The contextual keyword ‘var’ may only appear within a local variable declaration
You cannot use var as a return type.If you try to run the application with this code below you will receive the same error as before.
public var mygreatmethod()
{
return true;
}
Obviously you can’t do this. The variable must be initialised.
public void mygreatmethod()
{
var myname;
}
You cannot set a variable to null.
public void mygreatmethod()
{
var mynum=null;
}
Hopefully now you know what you can and cannot do with implicitly typed local variables
[…] would suggest that you have a look at this post of mine that talks about object initialisers and this one that talks about implicitly local […]