一成不变 [英] immutable

查看:99
本文介绍了一成不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中创建不可变对象?


提前感谢

解决方案



一个类,其数据在构造函数中提供,只有

只读属性是不可变的。例如:


class IdNamePair {

private int id;

私人字符串名称;


public IdNamePair(int id,string name){

this.id = id;

this.name = name;

}


public int Id {get {return id; } $

public string Name {get {return name; } $

}


是不可变的,因为一旦设置了

就无法更改ID或名称在构造函数上。你可以而且也应该在字段声明中指定readonly

,但是我想把它留在这里

强调只读属性是什么使这个类

不可变。


HTH,


Sam

------- -------------------------------------------------- ---

我们正在招聘! B-Line Medical正在寻求Mid / Sr. .NET

开发人员在医疗产品中获得令人兴奋的职位

开发MD / DC。在轻松的团队环境中使用各种技术

。在Dice.com上查看广告。


2006年11月1日星期三18:50:18 -0000,fallenidol

< fa ** ******@discussions.microsoft.comwrote:


>你如何在c#中创建一个不可变对象?

谢谢提前





语言中没有任何内容可以检查您打算制作的课程

inmutable实际上是不可变的。


目前inmutable更像是一个概念,它只是一个类它的

状态在初始化后不会改变。


这取决于你是否执行它。你只能获得属性和没有

方法可以产生副作用(更改内部成员)。


干杯,

IGnacio

fallidol写道:


如何在c#中创建一个不可变对象?


提前感谢





>

提供数据的类在构造函数中只有

只读属性是不可变的。例如:



没有,如果你有改变实例成员的方法,那么这个类

是不可能的。


您可以而且还应该在字段声明中指定readonly

,但我想把它留在这里

强调只读属性是使这个类

不可变的原因。



事实上,这可能是真正确定班级

将无法改变的唯一方法。

即便如此,也没有强制执行。以上作品的价值为

类型。在参考的情况下,您只能确保参考

不会变形。没有办法强制引用的实例确实不会改变。


how do you create an immutable object in c#?

thanks in advance

解决方案


An class whose data is provided in the constructor and has only
read-only properties is immutable. For example:

class IdNamePair {
private int id;
private string name;

public IdNamePair(int id, string name) {
this.id = id;
this.name = name;
}

public int Id { get { return id; } }
public string Name { get { return name; } }
}

Is immutable because there is no way to change id or name once they''ve
been set on the constructor. You can and should also specify readonly
on the field declarations but I wanted to leave that out here to
reinforce that the read-only properties are what make this class
immutable.

HTH,

Sam
------------------------------------------------------------
We''re hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 1 Nov 2006 18:50:18 -0000, "fallenidol"
<fa********@discussions.microsoft.comwrote:

>how do you create an immutable object in c#?

thanks in advance


Hi,

There is nothing in the language that check if a class you intended to make
inmutable is in reality inmutable.

Currently inmutable is more like a concept, it''s simply a class that its
state does not change after it was initialized.

It''s up to you do enforce it. You can only have "get" properties and none
method can have a side effect ( change the internals members).

Cheers,
IGnacio

"fallenidol" wrote:

how do you create an immutable object in c#?

thanks in advance


Hi,

>
An class whose data is provided in the constructor and has only
read-only properties is immutable. For example:

No really, if you have method that changes the instance members the class
is not inmutable.

You can and should also specify readonly
on the field declarations but I wanted to leave that out here to
reinforce that the read-only properties are what make this class
immutable.


As a matter of fact that maybe the only way to really be sure that the class
will be inmutable.
And even so there is no enforcement of that. The above works for valued
types. In the case of the references you can only assure that the reference
will not chage. There is no way to enforce that the referenced instance does
not change.


这篇关于一成不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆