如何有效地实现不可变类型
在编写 C# 代码时,我经常发现自己实现了不可变类型。 我总是最终编写大量代码,我想知道是否有更快的方法来实现它。
我通常写的内容:
public struct MyType
{
private Int32 _value;
public Int32 Value { get { return _value;} }
public MyType(Int32 val)
{
_value = val;
}
}
MyType alpha = new MyType(42);
当字段数量增加并且需要大量输入时,这会变得相当复杂。 有没有更有效的方法来做到这一点?
When coding C# I often find myself implementing immutable types.
I always end up writing quite a lot of code and I am wondering whether there is a faster way to achieve it.
What I normally write:
public struct MyType
{
private Int32 _value;
public Int32 Value { get { return _value;} }
public MyType(Int32 val)
{
_value = val;
}
}
MyType alpha = new MyType(42);
This gets fairly complicated when the number of fields grows and it is a lot of typing.
Is there a more efficient way for doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议编写更少代码的唯一方法是使用 ReSharper 之类的工具来自动生成代码。如果您从以下内容开始:
您可以生成“只读属性”以给出:
后跟生成构造函数以给出:
生成步骤总共是 8 个按键。
如果你真的想要一个不可修改的不可变类,我会这样声明它:
这使得该类不可派生(意味着子类无法修改其内部状态),并且
_value
属性只能分配施工期间。不幸的是,ReSharper 没有针对此模式的代码生成功能,因此您仍然需要手动构建(大部分)它。The only way I can suggest of writing less code is to use something like ReSharper to auto-generate the code for you. If you start with something like:
you can then generate "read-only properties" to give:
followed by generate constructor to give:
The generation steps are 8 key presses in total.
If you really want an unmodifiable immutable class, I would declare it as such:
This makes the class non-derivable (meaning that a sub-class cannot modify its inner state), and the
_value
property assignable only during construction. Unfortunately, ReSharper doesn't have code generation for this pattern, so you would still have to construct (most of) it manually.您可以使用自动属性和私有设置器稍微简化它,如下所示:
You could simplify it a little with automatic properties and a private setter as below:
救援代码片段!
将此 xml 保存为“immutable.snippet”,然后转到 Visual Studio,选择“工具”、“代码片段管理器”并导入它。就是这样!现在写“immutable”并按 TAB 两次,你就得到了不可变类型。
代码片段中的实际代码基于@adrianbanks 的答案。
Code snippets at the rescue!
Save this xml as "immutable.snippet", then go to Visual Studio, select Tools, Code Snippets Manager and import it. That's it! Now write "immutable" and hit TAB twice and you have your immutable type.
The actual code in the snippet is based on @adrianbanks answer.