初始化 C# 自动属性
我习惯于编写这样的类:
public class foo {
private string mBar = "bar";
public string Bar {
get { return mBar; }
set { mBar = value; }
}
//... other methods, no constructor ...
}
Converting Bar to an auto-property 看起来方便又简洁,但是如何在不添加构造函数并将初始化放在那里的情况下保留初始化呢?
public class foo2theRevengeOfFoo {
//private string mBar = "bar";
public string Bar { get; set; }
//... other methods, no constructor ...
//behavior has changed.
}
您可以看到,添加构造函数与我应该从自动属性中节省的精力并不相符。
这样的事情对我来说更有意义:
public string Bar { get; set; } = "bar";
I'm used to writing classes like this:
public class foo {
private string mBar = "bar";
public string Bar {
get { return mBar; }
set { mBar = value; }
}
//... other methods, no constructor ...
}
Converting Bar to an auto-property seems convenient and concise, but how can I retain the initialization without adding a constructor and putting the initialization in there?
public class foo2theRevengeOfFoo {
//private string mBar = "bar";
public string Bar { get; set; }
//... other methods, no constructor ...
//behavior has changed.
}
You could see that adding a constructor isn't inline with the effort savings I'm supposed to be getting from auto-properties.
Something like this would make more sense to me:
public string Bar { get; set; } = "bar";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过类的构造函数来完成此操作:
如果您有另一个构造函数(即,带有参数的构造函数)或一堆构造函数,您始终可以使用此构造函数(称为构造函数链接):
如果您始终将调用链接到默认构造函数,您可以在那里设置所有默认属性初始化。 链接时,将在调用构造函数之前调用链接的构造函数,以便更专业的构造函数能够根据需要设置不同的默认值。
You can do it via the constructor of your class:
If you've got another constructor (ie, one that takes paramters) or a bunch of constructors you can always have this (called constructor chaining):
If you always chain a call to the default constructor you can have all default property initialization set there. When chaining, the chained constructor will be called before the calling constructor so that your more specialized constructors will be able to set different defaults as applicable.
这在 C# 6.0 中将成为可能:
This will be possible in C# 6.0:
在默认构造函数中(当然还有任何非默认构造函数,如果您也有的话):
我相信这并不比您的原始代码性能差,因为无论如何这都是幕后发生的事情。
In the default constructor (and any non-default ones if you have any too of course):
This is no less performant that your original code I believe, since this is what happens behind the scenes anyway.
更新 - 下面的答案是在 C# 6 出现之前写的。 在 C# 6 中,您可以编写:
您还可以编写只读的自动实现的属性,这些属性只能在构造函数中写入(但也可以指定默认初始值):
不幸的是,没有现在这样做的方法。 您必须在构造函数中设置该值。 (使用构造函数链可以帮助避免重复。)
自动实现的属性现在很方便,但肯定会更好。 我发现自己并不像只读自动实现的属性那样经常需要这种初始化,该属性只能在构造函数中设置,并且由只读字段支持。
这种情况直到 C# 5(包括 C# 5)才发生,但正在计划在 C# 6 中实现 - 既允许在声明点进行初始化,又允许自动实现只读属性在构造函数体内初始化。
Update - the answer below was written before C# 6 came along. In C# 6 you can write:
You can also write read-only automatically-implemented properties, which are only writable in the constructor (but can also be given a default initial value):
It's unfortunate that there's no way of doing this right now. You have to set the value in the constructor. (Using constructor chaining can help to avoid duplication.)
Automatically implemented properties are handy right now, but could certainly be nicer. I don't find myself wanting this sort of initialization as often as a read-only automatically implemented property which could only be set in the constructor and would be backed by a read-only field.
This hasn't happened up until and including C# 5, but is being planned for C# 6 - both in terms of allowing initialization at the point of declaration, and allowing for read-only automatically implemented properties to be initialized in a constructor body.