详解C#中使用对象或集合的初始值设定项初始化的操作
使用对象初始值设定项初始化对象
可以使用对象初始值设定项以声明方式初始化类型对象,而无需显式调用类型的构造函数。
下面的示例演示如何将对象初始值设定项用于命名对象。编译器通过先访问默认实例构造函数然后处理成员初始化处理对象初始值设定项。因此,如果默认构造函数在类中声明为private,那么需要公共访问权的对象初始值设定项将失败。
下面的示例演示如何使用对象初始值设定项初始化新的StudentName类型。
publicclassProgram { publicstaticvoidMain() { //DeclareaStudentNamebyusingtheconstructorthathastwoparameters. StudentNamestudent1=newStudentName("Craig","Playstead"); //Makethesamedeclarationbyusinganobjectinitializerandsending //argumentsforthefirstandlastnames.Thedefaultconstructoris //invokedinprocessingthisdeclaration,nottheconstructorthathas //twoparameters. StudentNamestudent2=newStudentName { FirstName="Craig", LastName="Playstead", }; //DeclareaStudentNamebyusinganobjectinitializerandsending //anargumentforonlytheIDproperty.Nocorrespondingconstructoris //necessary.Onlythedefaultconstructorisusedtoprocessobject //initializers. StudentNamestudent3=newStudentName { ID=183 }; //DeclareaStudentNamebyusinganobjectinitializerandsending //argumentsforallthreeproperties.Nocorrespondingconstructoris //definedintheclass. StudentNamestudent4=newStudentName { FirstName="Craig", LastName="Playstead", ID=116 }; System.Console.WriteLine(student1.ToString()); System.Console.WriteLine(student2.ToString()); System.Console.WriteLine(student3.ToString()); System.Console.WriteLine(student4.ToString()); } }
这段的输出是:
Craig0 Craig0 183 Craig116
publicclassStudentName { //Thedefaultconstructorhasnoparameters.Thedefaultconstructor //isinvokedintheprocessingofobjectinitializers. //Youcantestthisbychangingtheaccessmodifierfrompublicto //private.ThedeclarationsinMainthatuseobjectinitializerswill //fail. publicStudentName(){} //Thefollowingconstructorhasparametersfortwoofthethree //properties. publicStudentName(stringfirst,stringlast) { FirstName=first; LastName=last; } //Properties. publicstringFirstName{get;set;} publicstringLastName{get;set;} publicintID{get;set;} publicoverridestringToString() { returnFirstName+""+ID; } }
下面的示例演示如何使用集合初始值设定项初始化一个StudentName类型集合。请注意,集合初始值设定项是一系列由逗号分隔的对象初始值设定项。
List<StudentName>students=newList<StudentName>() { newStudentName{FirstName="Craig",LastName="Playstead",ID=116}, newStudentName{FirstName="Shu",LastName="Ito",ID=112}, newStudentName{FirstName="Gretchen",LastName="Rivas",ID=113}, newStudentName{FirstName="Rajesh",LastName="Rotti",ID=114} };
使用集合初始值设定项初始化字典
Dictionary<TKey, TValue>包含键/值对集合。它的Add方法采用两个参数,一个用于键,另一个用于值。若要初始化Dictionary<TKey, TValue>或其Add方法采用多个参数的任何集合,请将每组参数括在大括号中,如下面的示例所示。
示例
在下面的代码示例中,使用StudentName类型的实例初始化一个Dictionary<TKey, TValue>。
classStudentName { publicstringFirstName{get;set;} publicstringLastName{get;set;} publicintID{get;set;} } classCollInit { Dictionary<int,StudentName>students=newDictionary<int,StudentName>() { {111,newStudentName{FirstName="Sachin",LastName="Karnik",ID=211}}, {112,newStudentName{FirstName="Dina",LastName="Salimzianova",ID=317}}, {113,newStudentName{FirstName="Andy",LastName="Ruth",ID=198}} }; }
请注意集合的每个元素中的两对大括号。最内层的大括号括起了StudentName的对象初始值,而最外层的大括号括起了将要添加到studentsDictionary<TKey, TValue>中的键/值对的初始值。最后,字典的整个集合初始值括在一对大括号内。