C# 中的不可变类型和不可变属性是什么意思?你能举个简单的例子吗?
不可变类型是其属性只能在初始化时设置的类型。一旦创建了对象,就无法再更改任何内容。不可变属性只是只读属性。
在下面的示例中,ImmutableType
是一种具有一个属性的不可变类型Test
。测试是只读属性。它只能在构造时设置。
class ImmutableType
{
private readonly string _test;
public string Test
{
get { return _test; }
}
public ImmutableType(string test)
{
_test = test;
}
}
See also: 维基百科文章 http://en.wikipedia.org/wiki/Immutable_object, and 一些堆栈 https://stackoverflow.com/questions/279507/what-is-meant-by-immutable 溢出问题 https://stackoverflow.com/questions/352471/how-do-i-create-an-immutable-class关于这个话题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)