C#中的类初始化器之间的区别? [英] Difference between class initializers in C#?
本文介绍了C#中的类初始化器之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用实例化对象之间有什么区别
What is the difference between instatiating an object by using
classInstance = new Class(){prop1 =",prop2 ="};
和
classInstance = new Class {prop1 =",prop2 ="};
推荐答案
没什么.第二个只是第一个的捷径.第一个允许您将参数包含到构造函数中.因此,如果类没有空的构造函数,则不能使用快捷方式.
Nothing. The second is just a short-cut for the first. The first allows you to include arguments to a constructor. So, you can't use the short-cut if the class doesn't have an empty constructor.
您可能对此问题感兴趣:
You may have an interest in this question:
埃里克·利珀特(Eric Lippert)的精彩博文:
And Eric Lippert´s great blog post:
这篇关于C#中的类初始化器之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文