Click here to Skip to main content
15,740,620 members
Articles / LINQ
Posted 17 Jun 2009

Tagged as


6 bookmarked

A little var Love

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
17 Jun 2009CPOL2 min read
A little var love

As we all know, one of the things that LINQ gives us is anonymous types that can be used by using the selection query operator, such as:

1:  var x = new { DateNow = DateTime.Now };

Which will give us an anonymous type with a single DateTime property called "DateNow", which is all cool. This is largely thanks to the "var" keyword.

If you spend time working with the var keyword, you will soon realise that you can no longer work with the object that is declared as a "var" as a strongly typed object outside the scope of the current method.

We can get around this by using the object type as a parameter where an anonymous parameter could be passed. This is demonstrated in the following screen shot. As we can see, we are able to pass an anonymous type, but from there, we have an object type, so the only way to get at the values is by using Reflection.


Here is an example that shows we can obtain the value of anonymous types, well enough.


Anonymous can't be used as method results, unless the method return type is object. This is why anonymous types should only be used locally within methods. One place where this rule is broken is within generic methods.

Consider the following example code:

 1:          /// <summary>
 2:          /// Simple generic method
 3:          /// </summary>
 4:          public static TResult
 5:              HaveSomeGenericFun<TResult>(Func<TResult> input)
 6:          {
 7:              return input();
 8:          }
10:          static void Main(string[] args)
11:          {
12:              //I am calling a generic method here passing it an
13:              //anonomous type….curious
14:              var obj = HaveSomeGenericFun(
15:                  () => new
16:                      {
17:                          Time = DateTime.Now,
18:                          ProcessName =
19:                              Process.GetCurrentProcess().ProcessName
20:                      });
22:              Console.WriteLine(String.Format(
23:                  "Process {0} was running at {1}",
24:                  obj.Time, obj.ProcessName));
26:              Console.ReadLine();
27:          }

The return type of the HaveSomeGenericFun is generic. If we call it without specifying a type for TResult, it is automatically inferred from the signature of the "input" parameter. Because the "input" Func<TResult> provided as an argument returns an instance of an anonymous type, the HaveSomeGenericFun() generic method returns that type.

As can be seen that the HaveSomeGenericFun() method doesn't use an object return value anywhere, the return value will be strongly typed.

Whilst this is all well and good and pretty useless by and large, LINQ uses this mechanism internally, and I just thought it was pretty interesting so thought it may be worth a mention.

So there you have it, consider it mentioned.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.


I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

-- There are no messages in this forum --