对象初始化程序和构造函数有什么区别?(What's the difference between an object initializer and a constructor?)

编程入门 行业动态 更新时间:2024-10-27 08:25:49
对象初始化程序和构造函数有什么区别?(What's the difference between an object initializer and a constructor?)

在“构造函数”之间使用“对象初始化程序”,反之亦然? 我正在使用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中,以便在使用对象时简化对象的构造。

赋值为0或更多参数的构造函数运行,用于在调用方法获取创建对象的句柄之前创建和初始化一个对象。 例如:

MyObject myObjectInstance = new MyObject(param1, param2);
 

在这种情况下, MyObject的构造函数将以值param1和param2 。 这些都用于在内存中创建新的MyObject 。 创建的对象(使用这些参数设置)被返回,并设置为myObjectInstance 。

一般来说,让构造函数需要所需的参数才能完全设置一个对象,这是不可能的,因此无法创建一个无效状态的对象。

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

这将导致对象初始化器 - 对象初始化程序允许您在对象的构造设置属性或字段,但使用其他任何东西之前。 例如:

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

这样做会像你这样做的一样:

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

然而,在多线程环境中,对象初始化器的原子性可能是有益的,因为它可以防止对象处于未完全初始化状态(有关更多详细信息,请参阅此答案 ) - 它与您打算一样为空或初始化。

此外,对象初始化器更容易读取(特别是当您设置多个值时),因此它们可以为构造函数中的许多重载提供同样的优势,而不需要使许多重载使该类的API复杂化。

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.

更多推荐

本文发布于:2023-08-05 19:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1438080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   有什么区别   函数   对象   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!