C#3.0提供了一个新的语法,对象初始化器(Object Initializers)。 该语法是为了简化一个常见的编程模式:构造一个新对象,逐个设置对象的公共属性。
实例如下:
class Program { static void Main(string[] args) { //对象初始化器 Person p = new Person() { FirstName = "Hello", LastName = "World" }; Console.WriteLine(p); Console.ReadKey(); } } class Person { public string FirstName { get;set; } public string LastName { get;set; } public override string ToString() { return string.Format("{0} {1}", FirstName, LastName); } }
从一开始我就知道这个语法等价于下面的语句:
Person p = new Person(); p.FirstName = "Hello"; p.LastName = "World"; ...
我自然也就认为编译器也会用这样的代码进行替换。偶然一次起了好奇心, 想看看编译器是不是真的这么做的,于是就查看生成的IL,结果真发现了点东西。IL代码我只能看个大概, 下面是在Reflector后的结果(选项设置为C#, .net2.0):
Person <>g__initLocal0 = new Person(); <>g__initLocal0.FirstName = "Hello"; <>g__initLocal0.LastName = "World"; Person p = <>g__initLocal0; Console.WriteLine(p); Console.ReadKey();
多了个局部变量了<>g__initLocal0, 构建的对象完成所有属性设置后再赋值给p。虽然我不理解为什么要加一个"临时变量", 但是这不会影响代码的最终结果。
原本到此就该结束了,但是在好奇心的驱使下又做了一个新的测试。 我把Person的定义改成了struct,这样他就是个值类型了。对象初始化器的语法对值类型依然有效,Reflector的结果没有变化, 但是这个赋值语句的意义就不一样了:
2021年4月12日 21:12
If you set out to make me think today; mission accomplished! I really like your writing style and how you express your ideas. Thank you. 123movies websites
2022年9月18日 01:39
Department of Education and Secondary Education Board has designed the AP SSC Civics Model Paper 2023 Pdf with answers for Telugu Medium, English Medium & Urdu Medium Students of the State Board. Every year there are a huge number of teaching staff and educational portals of the state have suggested the practice question bank with revision questions for both medium students of the board. AP 10th Civics Question Paper In civics, students learn to contribute to public processes and discussions of real issues. Students can also learn civic practices such as voting, volunteering, jury service, and joining with others to improve society. Civics enables students not only to study how others participate but also to practice participating and taking informed action themselves.
2023年1月15日 13:21
The C# object initializer syntax is a convenient way to create and initialize an object in one line of code. This syntax can be used with any object, including built-in types and user-defined cbd oil reviews types. To use the object initializer syntax, you simply specify the name of the object, followed by a set of curly braces containing the initial values for the object's properties or fields.
2024年4月14日 01:54
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. Orlando Bed Bug Pest Control
2024年4月14日 01:54
There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. Oviedo Bed Bug Pest Control
2024年4月15日 04:07
This is very appealing, however , it is very important that will mouse click on the connection: Kissimmee Termite Control
2024年8月28日 20:16
Gives you the best website address I know there alone you'll find how easy it is. قفل ذكي
2024年10月07日 12:44
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. Virtual CPA Services