Anonymous Type
In C#, an
anonymous type is a type (class) without any name that can contain public
read-only properties only. It cannot contain other members, such as methods, events, etc.You create an
anonymous type using the new keyword with an object initializer syntax. The implicitly typed variable- var is used
to hold the reference of anonymous types.
Example
Example
var student = new {
Id = 1,
FirstName = "Md",
LastName = "Rasel",
Address = "Dhaka"
};
Important Points
1. It is derived
from System.Object class.
2. It also has intelligence support in Visual Studio.
3. It can contain one or more read-only properties.
4. You can also create an anonymous type array.
5. It is of reference type.
Limitations
1. It does not contain class members such as events,
methods, etc.
2. The expression that used to initialize properties are
not null, anonymous method, or a pointer type.
3. It cannot be cast to any other type except an object.
4. You are not allowed to create a field, property,
event, or return type of a method is of anonymous type.
5. You are not allowed to declare formal parameters of a
method, property, constructor, indexer as a anonymous type.
No comments:
Post a Comment