Wednesday, 22 April 2009

Returning a "var" type from a method

The "var" type is a new introduction in .Net 3.x and it is also well known as an implicitly typed variable. The syntactical constraints are that they can be declared

1. Only inside a method.
2. only in a local scope.
3. only inside the scope of a method call.
4. only in an initialized state before being used.

So, if you require a "var" type returned from a method, you can do as below:

public class MyVar{
public static object VarReturningMethod(){
var implicitVar=new{LastName="Jv",
object nestedGenericVar = implicitVar;
return nestedGenericVar;
static void Main(){
var getVar=VarReturningMethod();

No comments: