Important
Anonymous types can be used to initialize simple types
like integers and strings.
Rules
Following are the some basic rules to use LINQ
Anonymous Types.
1. Anonymous types can't be
null.
2. Must always have an initial
assignment.
3. Can be used with Simple or
complex types, but composite anonymous types require member declaration
example
var mylist = new {Topic="Anonymous-Types" [, declaratory =
value, ...]}
In above code, Topic is the member declaratory.
4. Supports intellisense.
5. Cannot be used for class
field.
6. Can be used with arrays.
7. Anonymous types are all derived
from the Object type.
Now, lets explore some category or types of anonymous
types:
Simple Anonymous Type
With the keyword var and giving the value of the
variable in the right side of the assignment operator (=), anyone can declare
the Simple anonymous type.
var list = "Anonymous Types in LINQ";
The anonymous type is assigned to the name on the left
side of the assignment operator, and the type emitted by the compiler to
Microsoft Intermediate Language (MSIL) is determined by the right side of the
operator.
The above line is identical in the MSIL if defined as
following:
var list = "Anonymous Types in LINQ";
|