My understanding of. NET anonymous objects is as follows:
I. INTRODUCTION of the concept of anonymous objects in the NET3.5 era
In. NET3.0, Microsoft added anonymous objects to. net. Javascript programmers are no stranger to anonymous objects. But for the. NET programmers at that time, it should be a new thing .. NET4.0 allows us to instantiate an object without defining classes for this object. To understand how to "instantiate an object without defining classes for this object", use code to explain it:
Private static Object GetAnonymousObj () {Object anonymousObj = new {UserName = "admin", Password = "1111"}; return anonymousObj ;}
No related classes are created for the anonymousObj object before the instantiation object. Therefore, the anonymousObj object is called an anonymous object.
II. In. NET 4.0, anonymous objects are used as function return values.
Return to the preface introduced at the beginning of this article-can an anonymous object be used as a function return value? In. NET4.0, this has become a reality.
Take a look at the sample code and use an anonymous object as the return value:
Private static Object GetAnonymousObj () {Object anonymousObj = new {UserName = "admin", Password = "1111"}; return anonymousObj ;}
A simple piece of code allows us to use the anonymousObj anonymous object as the function return value. But we found a problem when calling this function. We cannot do this:
Object obj = GetAnonymousObj (); Console. WriteLine (obj. UserName); // compilation fails here. Because obj objects are of the Object type.
Therefore, in. net3.5, I told my colleagues that this should not be implemented in this way. How can I change it? When the era of. NET4.0 came, our problems were properly solved-first of all, the Dynamic type debuted: (Dynamic brief introduction)
First, convert the GetAnonymousObj function in the preceding section to the dynamic type returned:
Private static dynamic GetAnonymousObj () {dynamic anonymousObj = new {UserName = "admin", Password = "1111"}; return anonymousObj ;}
Call the GetAnonymousObj () method:
Static void Main (string [] args) {dynamic o = GetAnonymousObj (); Console. WriteLine (o. UserName); Console. ReadKey ();}
Output> admin.
Anonymous objects may not be used in many projects, but most of my projects are based on LinqToSQL, so we often write this code: use an anonymous object in a LINQ query statement. For example:
Var user = from user in Users select new {user. Name, user. Id} // Anonymous object
III. Other considerations for anonymous objects in. NET
Anonymous Objects inherit from System. Object.
All anonymous object attributes are read-only ).
In the same Assembly, two anonymous objects have the same attributes and the same order of attributes. The compiler will think that the two anonymous objects are the same.
Use of anonymous objects in. net
Js syntax:
Var list = []; var o = {}; o. id = '000000'; o. name = '000000'; list. push (o );
C:
Var aList = new List <dynamic> (); dynamic o = new System. dynamic. expandoObject (); o. id = "111"; o. name = "222"; aList. add (o );