什么是对象初始化和构造函数之间的区别? [英] What's the difference between an object initializer and a constructor?

查看:220
本文介绍了什么是对象初始化和构造函数之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这两个之间的区别时,你会在一个构造,反之亦然使用对象初始化?我正在用C#,如果该事项。此外,特定于C#的对象初始化方法或.NET?

What are the differences between the two and when would you use an "object initializer" over a "constructor" and vice-versa? I'm working with C#, if that matters. Also, is the object initializer method specific to C# or .NET?

推荐答案

对象初始化是当你使用一个对象的东西加入到C#3,为了简化施工的对象。

Object Initializers were something added to C# 3, in order to simplify construction of objects when you're using an object.

构造来看,给出0或多个参数,以及用于创建和初始化对象的的呼叫方法得到的句柄创建的对象。例如:

Constructors run, given 0 or more parameters, and are used to create and initialize an object before the calling method gets the handle to the created object. For example:

MyObject myObjectInstance = new MyObject(param1, param2);

在这种情况下,为MyObject 的构造将与值执行参数1 参数2 。这些都是用于创建新的为MyObject 在内存中。创建的对象(这是安装程序使用这些参数)被退回,并设置为 myObjectInstance

In this case, the constructor of MyObject will be run with the values param1 and param2. These are both used to create the new MyObject in memory. The created object (which is setup using those parameters) gets returned, and set to myObjectInstance.

在一般情况下,它被认为很好的做法,有一个构造函数需要必要的,以完全建立一个对​​象的参数,这样是不可能的无效状态创建一个对象。

In general, it's considered good practice to have a constructor require the parameters needed in order to completely setup an object, so that it's impossible to create an object in an invalid state.

然而,经常有可能进行设置,但并不需要额外的属性。这可以通过重载的构造进行处理,但会导致具有大量的构造不一定是在大多数情况下是有用的。

However, there are often "extra" properties that could be set, but are not required. This could be handled through overloaded constructors, but leads to having lots of constructors that aren't necessarily useful in the majority of circumstances.

这导致了对象初始化 - 对象初始值设定让您的的它已经构建设置你的对象的属性或字段,但是的的你可以什么都用它。例如:

This leads to object initializers - An Object Initializer lets you set properties or fields on your object after it's been constructed, but before you can use it by anything else. For example:

MyObject myObjectInstance = new MyObject(param1, param2)
{
    MyProperty = someUsefulValue
};

这将表现大致相同,如果你这样做:

This will behave about the same as if you do this:

MyObject myObjectInstance = new MyObject(param1, param2);
myObjectInstance.MyProperty = someUsefulValue;

然而,在多线程的环境对象初始化的原子可能是有益的,因为它$ P $从一个不完全初始化状态的pvents对象(见的这个答案了解更多详细信息) - 这是null或类似初始化你意。

However, in multi-threaded environments the atomicity of the object initializer may be beneficial, since it prevents the object from being in a not-fully initialized state (see this answer for more details) - it's either null or initialized like you intended.

此外,对象初始化更简单阅读(特别是当您设置多个值),让他们给你同样的好处,因为在构造许多重载,而不需要有许多重载该类的API复杂。

Also, object initializers are simpler to read (especially when you set multiple values), so they give you the same benefit as many overloads on the constructor, without the need to have many overloads complicating the API for that class.

这篇关于什么是对象初始化和构造函数之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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