C#:通过构造函数与实例化将数据分配给属性 [英] C# : assign data to properties via constructor vs. instantiating

查看:124
本文介绍了C#:通过构造函数与实例化将数据分配给属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Album类:

public class Album 
{
    public string Name {get; set;}
    public string Artist {get; set;}
    public int Year {get; set;}

    public Album()
    { }

    public Album(string name, string artist, int year)
    {
        this.Name = name;
        this.Artist = artist;
        this.Year = year;
    }
}

当我想为类型为Album的对象分配数据时,接下来的两种方法之间有什么区别:

When I want to assign data to an object of type Album, what is the difference between the next 2 approaches :

通过构造函数

var albumData = new Album("Albumius", "Artistus", 2013);

实例化时

var albumData = new Album 
                    {
                         Name = "Albumius",
                         Artist = "Artistus",
                         Year = 2013
                    };

推荐答案

两种方法都调用构造函数,而只是调用不同的方法.这段代码:

Both approaches call a constructor, they just call different ones. This code:

var albumData = new Album 
                {
                     Name = "Albumius",
                     Artist = "Artistus",
                     Year = 2013
                };

是此等效代码的语法简写:

is syntactic shorthand for this equivalent code:

var albumData = new Album();
albumData.Name = "Albumius";
albumData.Artist = "Artistus";
albumData.Year = 2013;

这两个在编译后是相同.因此,如果无参数构造函数不是公开的:

The two are identical after compilation. So if the parameterless constructor wasn't public:

public Album() { }

那么您将根本无法使用对象初始化器. 所以主要问题不是初始化对象时要使用哪个,而是对象首先要公开哪个构造函数.如果对象公开了两个构造函数(如示例中的那个) ,则可以假定两种方法对于构造对象都是同等有效的.

then you wouldn't be able to use the object initializer at all anyway. So the main question isn't which to use when initializing the object, but which constructor(s) the object exposes in the first place. If the object exposes two constructors (like the one in your example), then one can assume that both ways are equally valid for constructing an object.

有时对象不会公开无参数的构造函数,因为它们需要某些构造值.尽管在这种情况下,您仍然可以对其他值使用初始化程序语法.例如,假设您的对象上具有以下构造函数:

Sometimes objects don't expose parameterless constructors because they require certain values for construction. Though in cases like that you can still use the initializer syntax for other values. For example, suppose you have these constructors on your object:

private Album() { }
public Album(string name)
{
    this.Name = name;
}

由于无参数构造函数是私有的,因此您不能使用它.但是您可以使用另一种,并且仍然可以使用初始化程序语法:

Since the parameterless constructor is private, you can't use that. But you can use the other one and still make use of the initializer syntax:

var albumData = new Album("Albumius")
                {
                     Artist = "Artistus",
                     Year = 2013
                };

编译后的结果将与以下内容相同:

The post-compilation result would then be identical to:

var albumData = new Album("Albumius");
albumData.Artist = "Artistus";
albumData.Year = 2013;

这篇关于C#:通过构造函数与实例化将数据分配给属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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