C# 中对象初始化器的解释?

发布于 2024-11-07 21:30:20 字数 78 浏览 0 评论 0原文

如果可能的话,有人可以提供 C# 中对象初始化程序的解释和示例吗?

到目前为止,我了解到它们允许您压缩类的实例,这是正确的吗?

Could someone please provide a explanation of Object Initialisers in C# and examples if possible.

So far I understand that they allow you to condense instances of a class, is this correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一直都在从未离去 2024-11-14 21:30:21

对象初始值设定项已添加到 C# 版本 3 中,并提供精简的对象初始化语法。它们只不过是语法糖,这意味着它们允许您表达比以前更短的相同代码。

这是一个示例:

Foo foo = new Foo { Name = "John", Age = 42 };

相当于:

Foo foo = new Foo();
foo.Name = "John";
foo.Age = 42;

在此处查看官方 文档

一个相关的功能是集合初始值设定项,它可以让您轻松初始化集合:

var names = new List<string> { "John", "Michael", "Joe" }

上面的代码是构造一个新列表,然后将每个元素添加到其中的简写。要使集合初始值设定项适用于某种类型,只需采用正确类型作为参数的 public Add 方法即可。

Object initializers was added to C# in version 3, and provide condensed object initialization syntax. They are nothing but syntactic sugar, meaning they allow you to express the same code shorter than before.

Here is an example:

Foo foo = new Foo { Name = "John", Age = 42 };

Which is the equivalent of:

Foo foo = new Foo();
foo.Name = "John";
foo.Age = 42;

Have a look on the official documentation here.

A related feature is collection initializers, which lets you initialize collections easily:

var names = new List<string> { "John", "Michael", "Joe" }

The above code is short hand for constructing a new List, and then adding each element to it. For collection initializers to work for a type, nothing more is required than a public Add method taking the correct type as the parameter.

蘑菇王子 2024-11-14 21:30:21

对象初始值设定项允许您在创建时将值分配给对象的任何可访问字段或属性,而无需显式调用构造函数。

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

请参阅对象和集合初始值设定项(C# 编程指南)

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

Refer to Object and Collection Initializers (C# Programming Guide).

不知所踪 2024-11-14 21:30:21

对象初始值设定项允许您在声明实例时设置实例的属性。例如:

class House
{
    public int Rooms { get; set; }
    public int PeopleLiving { get; set; }
}

然后使用这个:

House h = new House { Rooms = 4, PeopleLiving = 5 }

在创建对象时简单地设置这些值,有点像构造函数,您可以在其中选择要设置的属性。您还可以使用构造函数:

House h = new House("10 Some Road", "Some City") { Rooms = 4, PeopleLiving = 5 }

这实际上只是语法糖,编译器将其转换为等效的 C# 代码:

House h = new House();
h.Rooms = 4;
h.PeopleLiving = 5;

Object Initialisers allow you to set the properties of an instance when you declare it. For example:

class House
{
    public int Rooms { get; set; }
    public int PeopleLiving { get; set; }
}

Then using this:

House h = new House { Rooms = 4, PeopleLiving = 5 }

Simply sets these values when you create the object, a bit like a constructor where you can choose which properties to set. You can also use a constructor as well:

House h = new House("10 Some Road", "Some City") { Rooms = 4, PeopleLiving = 5 }

This is actually just syntactic sugar, the compiler converts it to this equivalent C# code:

House h = new House();
h.Rooms = 4;
h.PeopleLiving = 5;
玻璃人 2024-11-14 21:30:21

来自 MSDN

对象初始值设定项允许您在创建时为对象的任何可访问字段或属性分配值,而无需显式调用构造函数。

所有这些意味着它是一个方便的快捷方式,您可以使用它(希望)使您的代码更具可读性。他们给出的例子:

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

更传统地写为:

Cat cat = new Cat();
cat.Age = 10;
cat.Name = "Fluffy";

或者

Cat = new Cat(10, "Fluffy");

前者可能更冗长,并且编译后的代码之间存在差异(感谢 @Kirk Woll 获取链接),解释于 这篇文章。初始化程序创建一个临时变量,分配属性,然后将其复制到实际变量。类似这样:

Cat temporaryCat = new Cat();
temporaryCat.Age = 10;
temporaryCat.Name = "Fluffy";
Cat cat = temporaryCat;

后者(在带有可选参数的 C# 4.0 之前)要求您为每个可能的参数组合拥有许多重载版本的构造函数。

From the MSDN

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.

All this means is that it's a convenient shortcut you can use to (hopefully) make your code more readable. The example they give:

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

would be more traditionally written as:

Cat cat = new Cat();
cat.Age = 10;
cat.Name = "Fluffy";

or

Cat = new Cat(10, "Fluffy");

The former can be more long winded, and there is a difference between the compiled code (thanks to @Kirk Woll for the link), explained in this post. The initialiser creates a temporary variable, assigns the properties and then copies that to the real variable. Something like this:

Cat temporaryCat = new Cat();
temporaryCat.Age = 10;
temporaryCat.Name = "Fluffy";
Cat cat = temporaryCat;

The latter would (before C# 4.0 with optional arguments) require you to have a many overloaded versions of your constructor for each possible parameter combination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文