Anonymous types

An anonymous type is a combination of both object initializers and implicit typing. An anonymous type is a type that doesn't have a name. Using anonymous types, using the var and new keyword, we create an object without defining its type or class. The type of anonymous type variable is inferred based on the value with which it's initialized. Along with that, the properties of an anonymous variable are read-only, which means we cannot change their values after the variable has been initialized. 

The following is some sample code syntax where we have declared an object of an anonymous type. In the object, we have specified three properties, PropertyNum1, PropertyNum2, and PropertyNum3:

var anonymousType = new
{
PropertyNum1 = "One",
PropertyNum2 = 2,
PropertyNum3 = true
};
Console.WriteLine(anonymousType.GetType().ToString());

Once the code is executed, we get the following output:

Note that, as we are displaying the type of the anonymous type, for each of its respective properties, the execution is displaying the type based upon the value that is assigned to the property. Hence, the output that we see is String, Int32, and Boolean.

In the next section, we will look at some standard LINQ operators that we use often while writing LINQ queries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.222