10. What is C#?
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.
var str = "1"; var num = 0; string s = "string"; var s2 = s; s2 = null; string s3 = null; var s4 = s3;
string str = "1"; int num = 0; string s2 = s; string s4 = s3;
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 }; 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; }
}
} 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.