简体   繁体   English

不变变量和不变对象

[英]immutable variable and immutable object

What is the difference between immutable object and immutable variable ? 不可变对象不可变变量有什么区别? are they both unchangeable ? 他们都是不变的吗?

Immutable Variables are variables that cannot change at all. 不可变变量是完全不能更改的变量。 but for simple types is there another name or term for an immutable variable ? 但对于简单的类型是有另外一个名字术语 ,一个不变的变量 can we use "read only" or is there a specific term for it? 我们可以使用“只读”还是有特定术语

What is the difference between immutable object and immutable variable? 不可变对象和不可变变量有什么区别?

An immutable object is an object whose value (meaning its property values) cannot change, either via external accessors or through internal methods. 不变对象是其 (即其属性值)无法通过外部访问器或内部方法更改的对象。

Does C# have "immutable variables"? C#是否具有“不可变变量”?

If you consider class members "variables" then Yes. 如果您将班级成员视为“变量”,则可以。 If not, then No. C++ has the concept of "constant" local variables but C# does not. 如果不是,则否。C++具有“常量”局部变量的概念,但C#没有。

for simple types is there another name or term for an immutable variable? 对于简单类型,是否存在不可变变量的其他名称或术语?

Not in the "local variable" sense. 不是“局部变量”的意思。 In C# there are two types of class members that cannot change. 在C#中,有两种类型的类成员不能更改。 One is readonly , which can be set at run-time as part of object construction (or static construction for a static redaonly field). 一种是readonly ,可以在运行时将其设置为对象构造的一部分(对于static redaonly字段,则是static redaonly )。

The other is constant , which is similar to readonly , but the value of the constant is baked into the binary code of the class, meaning that any time the field is referenced the value is substituted into the compiled code. 另一个是constant ,类似于readonly ,但是常量的烘焙到该类的二进制代码中,这意味着每当引用该字段时,该值就会被替换为编译后的代码。

An immutable object is one that cannot be changed (with the expectation that you get the same object back). 一个不可变的对象是一个不能更改的对象(期望您可以返回相同的对象)。 This might be slightly confusing, so let me explain one of the simplest examples I can think of. 这可能有点令人困惑,所以让我解释一下我能想到的最简单的例子之一。 Say I have the following code: 说我有以下代码:

string myString = "Hello World!";

Strings are an example of immutable objects. 字符串是不可变对象的一个​​示例。 Now, obviously you can change the value of myString , however, you are not simply making changes to the myString object when you do this. 现在,很明显,您可以更改myString的值,但是,当您执行此操作时,您并不仅仅是更改myString对象。 The language actually creates a NEW string object that myString will point to. 该语言实际上创建了myString指向的NEW字符串对象。 (I cannot remember the exact details for how this works, for the reason why, see this answer ) (我不记得它是如何工作的确切细节,出于这个原因,请参阅此答案

//This is creating a new string object, although it looks like a simple
//manipulation of what currently exists.
myString = "Goodbye World!";  

Immutable variables (as you call them) on the other hand (commonly referred to as " constants " or " constant fields " in my experience), CANNOT change. 另一方面, 不可变变量 (如您所称)(在我的经验中通常称为“ 常量 ”或“ 常量字段 ”)不能更改。 Your compiler will complain if you attempt to modify it in any way. 如果您尝试以任何方式修改它,编译器都会抱怨。 There are many reasons why you'd want to do that (a Google search will give you dozens). 为什么要这样做有很多原因(Google搜索将为您提供许多帮助)。 In short, make a field constant where you know the value will never and should never be altered, even if by accident. 简而言之,将一个字段设为常量,使您知道该值将永远也不应更改,即使偶然发生也是如此。

This might be a weak example, but say you have a program that builds a Vehicle object and you know the most wheels you will ever need is 18. You could say: 这可能是一个微不足道的示例,但是假设您有一个构建Vehicle对象的程序,并且知道您将需要的最大车轮数是18。您可以说:

const int MAX_WHEELS = 18;

And that can never change by accident. 那永远不会偶然改变。 Any time you use MAX_WHEELS, it will always be 18. I do not know the details, but it seems likely that the compiler will just substitute value in for the field name, but maybe someone can correct me on that. 每当您使用MAX_WHEELS时,它将始终为18。我不知道细节,但是似乎编译器将只用value代替字段名,但是也许有人可以对此进行更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM