C# 中的构造函数与初始值设定项
可能的重复:
对象初始值设定项和构造函数之间有什么区别?< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只是语法糖(即简写)
两者之间的一个区别是您可以设置 readonly 构造函数中的字段,但不使用简写。
第二个区别是带有参数的构造函数强制客户端提供这些值。请参阅构造函数注入与 Setter 注入进行大量的背景阅读。
is just syntactic sugar (i.e. shorthand) for
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.
区别可能在于第二个无法编译。
您缺少一个默认构造函数,它在第二个示例中调用它。
The difference is probably that the second won't compile.
You are missing a default constructor, which it calls in your second example.
这是构造对象,并使用构造函数中的值执行某些操作。它可能会设置值,也可能不会。这取决于构造函数中的逻辑。
这是使用默认构造函数构造对象,并在构造对象后设置属性的初始值。在这种情况下,设置值与构造函数无关,除了它位于同一语句中之外。它与:
这种语法的巧妙之处在于,您可以执行:
而不是必须执行:
因为变量的初始化无需触及变量赋值。
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.
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:
What is neat about this syntax is that you can do:
instead of having to do:
because the initialization of the variables happens without touching the variable assignment.
第二种形式
是语法
,关键点是值的设置不是构造函数的一部分。
The second form
is the syntax for
the key point is that the setting of the values is not part of the constructor.
两者不是同一件事。
这是调用类构造函数:
这是一个内联对象初始值设定项:
首先确保创建 MyObject 实例在构造期间需要两个整数输入参数,而另一个仅在构造期间初始化一些字段或属性。最后一个保留不一定初始化此类属性和/或字段的自由。
Both aren't the same thing.
This is invoking class constructor:
And this is an inline object initializer:
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.
第一个是参数 ctor,
第二个是默认(零参数)ctor,但是一旦 ctor 完成,val1 和 val2 将使用提供的值进行初始化。
此外,您可以不带大括号调用第二个。例如
The first is a parameter ctor
The second is the default (zero parameter) ctor, but once the ctor finishes, val1 and val2 are initialized with the values provided
Also, you can call the second one w/o the braces. E.g.