通过设置属性值来创建新对象的2种方法 [英] 2 ways to create new object by setting property values

查看:46
本文介绍了通过设置属性值来创建新对象的2种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

大家好
我有一个类 Question ,它具有一个属性 Text

Hi all
I have a class Question which has a property Text

public class Question
{
    public string Text { get; set; }
}

现在,我想通过为属性赋值来创建这种类型的对象.
我可以通过两种方式做到这一点:

Now I want to create an object of this type by giving value to property.
I can do that in this two ways:

问题q =新问题{文本=某些问题"};

问题q =新问题(){文字=某些问题"};

这两种情况之间是否有区别?如果相同,为什么我们都需要?
谢谢.

Is there any difference between this two cases and if they are the same, why we need both?
Thanks.

推荐答案

两个示例之间绝对没有区别.

There's absolutely no difference between the two examples.

在这种情况下,并且仅在这种情况下,构造函数上的()是可选的.

In this case, and in this case alone, the () on the constructor is optional.

这篇关于通过设置属性值来创建新对象的2种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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