Please enable Javascript to correctly display the contents on Dot Net Tricks!

C Sharp Var data type and Anonymous Type

  Author : Shailendra Chauhan
Posted On : 06 Apr 2012
Total Views : 99,127   
Updated On : 20 May 2014
 

var data type was introduced in C# 3.0. var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time. A var variable must be initialized at the time of declaration.

Valid var statements

var str = "1"; 
var num = 0; 
string s = "string"; 
var s2 = s; 
s2 = null; 
string s3 = null; 
var s4 = s3; 

At compile time, the above var statements are compiled to IL, like this:

 string str = "1"; 
int num = 0; 
string s2 = s; 
string s4 = s3; 

The compile-time type value of var variable cannot be null but the runtime value can be null.

 // invalid var statements 
var v; //need to initialize
var num = null; // can’t be null at compile time 

Once var variable is initialized its data type became fixed to the type of the initial data.

 // invalid var statements 
var v2 = "str12";
 v2 = 3; // int value can’t be assign to implicitly type string variable v2 

Anonymous Types

An anonymous type is a simple class generated by the compiler within IL to store a set of values. var data type and new keyword is used to create an anonymous type.

 var emp = new { Name = "Deepak", Address = "Noida", Salary=21000 }; 

At compile time, the compiler will create an anonymous type, as follows:

 class __Anonymous1 
{ 
 private string name; 
 private string address; 
 int salary; public string Name
 { 
 get{return name; }
 set { name=value } 
 } 
 public string Address
 { 
 get{ return address; } 
 set{ address=value; } 
 } 
 public int Salary
 { 
 get{ return salary; } 
 set{ salary=value; } 
 } 
} 

The anonymous type is very useful when you want to shape the result in your desired form like this:

 var result =from book in Books
 where book.Price > 200 
 orderby book.IssueDate descending 
 select new 
 { 
 Name = book.Name, 
 IssueNumber = "#" + book.Issue
 }; 

In above example, I change the name of the “Issue” field of Book table to “IssueNumber” and add # before value to get desired output.

Summary

In this article I try to explain var data type with example. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.



Free Interview Books
 
COMMENTS (0)
6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+