C# 中的构造函数与初始值设定项

发布于 2024-11-02 10:18:24 字数 761 浏览 1 评论 0原文

可能的重复:
对象初始值设定项和构造函数之间有什么区别?< /a>

在 c# 中,您可以构造一个对象,例如:

public class MyObject{

     int val1;
     int val2;

     public MyObject(int val1, val2){
           this.val1 = val1;
           this.val2 = val2;
     }
}

With:

MyObject ob = new MyObject(1,2);

或 with:

MyObject ob = new MyObject(){ val1 = 1, val2 = 2 };

这种构造函数之间有什么区别?

Possible Duplicate:
What's the difference between an object initializer and a constructor?

In c# you can construct an object like:

public class MyObject{

     int val1;
     int val2;

     public MyObject(int val1, val2){
           this.val1 = val1;
           this.val2 = val2;
     }
}

With:

MyObject ob = new MyObject(1,2);

or with:

MyObject ob = new MyObject(){ val1 = 1, val2 = 2 };

What is the diference between that kind of constructors?

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

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

发布评论

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

评论(6

比忠 2024-11-09 10:18:24
MyObject ob = new MyObject(){ val1 = 1, val2 = 2 };

只是语法糖(即简写)

MyObject ob = new MyObject();
ob.val1 = 1;
ob.val2 = 2;

两者之间的一个区别是您可以设置 readonly 构造函数中的字段,但不使用简写。

第二个区别是带有参数的构造函数强制客户端提供这些值。请参阅构造函数注入与 Setter 注入进行大量的背景阅读。

MyObject ob = new MyObject(){ val1 = 1, val2 = 2 };

is just syntactic sugar (i.e. shorthand) for

MyObject ob = new MyObject();
ob.val1 = 1;
ob.val2 = 2;

One difference between the two is that you can set readonly fields from the constructor, but not by using the shorthand.

A second difference is that a constructor with parameters forces the client to provide those values. See Constructor-injection vs. Setter injection for a good bit of background reading.

若相惜即相离 2024-11-09 10:18:24

区别可能在于第二个无法编译。

您缺少一个默认构造函数,它在第二个示例中调用它。

The difference is probably that the second won't compile.

You are missing a default constructor, which it calls in your second example.

沉溺在你眼里的海 2024-11-09 10:18:24
MyObject ob = new Object(1,2);

这是构造对象,并使用构造函数中的值执行某些操作。它可能会设置值,也可能不会。这取决于构造函数中的逻辑。

MyObject ob = new Object(){ val1 = 1, val2 = 2 };

这是使用默认构造函数构造对象,并在构造对象后设置属性的初始值。在这种情况下,设置值与构造函数无关,除了它位于同一语句中之外。它与:

MyObject ob = new Object();
ob.val1 = 1;
...

这种语法的巧妙之处在于,您可以执行:

 Object ob = new Object(){ val1 = 1, val2 = 2 };

而不是必须执行:

Object ob = new Object(1,2);
((MyObject)ob).val1 = 1;  //note having to cast.
... 

因为变量的初始化无需触及变量赋值。

MyObject ob = new Object(1,2);

This is constructing the object, and doing something with the values in the constructor. It maybe setting the values, it may not. It depends on the logic in the constructor.

MyObject ob = new Object(){ val1 = 1, val2 = 2 };

This is constructing the object using the default constructor, and setting initial values for properties, after the object has been constructed. In this scenario, setting the values has nothing to do with the constructor besides the fact that it is in the same statement. It's the same as:

MyObject ob = new Object();
ob.val1 = 1;
...

What is neat about this syntax is that you can do:

 Object ob = new Object(){ val1 = 1, val2 = 2 };

instead of having to do:

Object ob = new Object(1,2);
((MyObject)ob).val1 = 1;  //note having to cast.
... 

because the initialization of the variables happens without touching the variable assignment.

独闯女儿国 2024-11-09 10:18:24

第二种形式

  MyObject ob = new Object(){ val1 = 1, val2 = 2 };

是语法

  MyObject ob = new Object();
  ob.val1 = 1;
  ob.val2 = 2;

,关键点是值的设置不是构造函数的一部分。

The second form

  MyObject ob = new Object(){ val1 = 1, val2 = 2 };

is the syntax for

  MyObject ob = new Object();
  ob.val1 = 1;
  ob.val2 = 2;

the key point is that the setting of the values is not part of the constructor.

以为你会在 2024-11-09 10:18:24

两者不是同一件事。

这是调用类构造函数:

MyObject ob = new Object(1,2);

这是一个内联对象初始值设定项:

MyObject ob = new Object(){ val1 = 1, val2 = 2 };

首先确保创建 MyObject 实例在构造期间需要两个整数输入参数,而另一个仅在构造期间初始化一些字段或属性。最后一个保留不一定初始化此类属性和/或字段的自由。

Both aren't the same thing.

This is invoking class constructor:

MyObject ob = new Object(1,2);

And this is an inline object initializer:

MyObject ob = new Object(){ val1 = 1, val2 = 2 };

First ensures creating an instance of MyObject requires two integer input parameters during construction time, while the other one, just initializes some fields or properties after construction time. Last one keeps freedom of not necessarily initialize such properties and/or fields.

徒留西风 2024-11-09 10:18:24

第一个是参数 ctor,

MyObject ob = new MyObject (1,2);

第二个是默认(零参数)ctor,但是一旦 ctor 完成,val1 和 val2 将使用提供的值进行初始化。

MyObject ob = new MyObject (){ val1 = 1, val2 = 2 };

此外,您可以不带大括号调用第二个。例如

MyObject ob = new MyObject { val1 = 1, val2 = 2 };

The first is a parameter ctor

MyObject ob = new MyObject (1,2);

The second is the default (zero parameter) ctor, but once the ctor finishes, val1 and val2 are initialized with the values provided

MyObject ob = new MyObject (){ val1 = 1, val2 = 2 };

Also, you can call the second one w/o the braces. E.g.

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