对象初始值设定项和构造函数之间有什么区别? [英] What's the difference between an object initializer and a constructor?

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

问题描述

两者之间有什么区别,您何时会在构造函数"上使用对象初始值设定项",反之亦然?如果这很重要,我正在使用 C#.另外,对象初始值设定项方法是特定于 C# 还是 .NET?

解决方案

对象初始值设定项是添加到 C# 3 中的内容,目的是在您使用对象时简化对象的构造.

构造函数运行,给定 0 个或多个参数,用于创建和初始化对象之前调用方法获取已创建对象的句柄.例如:

MyObject myObjectInstance = new MyObject(param1, param2);

在这种情况下,MyObject 的构造函数将使用值 param1param2 运行.它们都用于在内存中创建新的 MyObject.创建的对象(使用这些参数设置)被返回,并设置为 myObjectInstance.

一般来说,让构造函数需要所需的参数以完全设置对象被认为是一种很好的做法,这样就不可能在无效状态下创建对象.

但是,通常可以设置额外"属性,但不是必需的.这可以通过重载构造函数来处理,但会导致大量构造函数在大多数情况下不一定有用.

这导致了对象初始值设定项 - 对象初始值设定项允许您在对象被构造之后设置属性或字段,但是之前您可以通过其他任何方式使用它.例如:

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

这将与您执行此操作的行为大致相同:

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

但是,在多线程环境中,对象初始值设定项的原子性可能是有益的,因为它可以防止对象处于未完全初始化的状态(请参阅此答案 了解更多详细信息)- 它要么为空,要么按照您的意图进行初始化.

此外,对象初始值设定项更易于阅读(尤其是当您设置多个值时),因此它们为您提供与构造函数上的许多重载相同的好处,而无需使该类的 API 复杂化的许多重载.

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?

解决方案

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

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);

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;

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.

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天全站免登陆