C#中的类初始化器之间的区别? [英] Difference between class initializers in C#?

查看:68
本文介绍了C#中的类初始化器之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么C#3.0对象初始值设定项构造函数括号是可选的?

使用实例化对象之间有什么区别

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:

为什么C#3.0对象初始化程序构造函数的括号是可选的?

埃里克·利珀特(Eric Lippert)的精彩博文:

And Eric Lippert´s great blog post:

歧义的可选括号,部分一个

这篇关于C#中的类初始化器之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆