2

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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Alex Jones
  • 31
  • 1
  • 3
  • 5
    Surely "immutable variable" is an oxymoron: immutable = "cannot change", variable = "can change". – LukeH Oct 28 '15 at 17:31
  • Variables aren't really considered immutable, because by definition, they vary with context. – Ron Beyer Oct 28 '15 at 17:31
  • 1
    An immutable variable is one that cannot be reassigned, an immutable object is one that can't be mutated, so no they aren't the same. You can have an immutable variable pointing to a mutable object and a mutable variable assigned to an immutable object. – Lee Oct 28 '15 at 17:32
  • 1
    Even if you are talking about constant variables, I've never really heard anyone call them "immutable variables" – Broots Waymb Oct 28 '15 at 17:32
  • 2
    constant variable is true Paradox ;) @DangerZone – M.kazem Akhgary Oct 28 '15 at 17:33
  • @DangerZone immutable variable has an assign-once property and cannot be altered or unbound programmatically once bound to a value... but my actual question is is there another term for immutable variable? can we call them read only variables or is there a specific term for it? – Alex Jones Oct 28 '15 at 17:39
  • Even though they are "variables"/fields in the programming (NOT English) sense, I hear them referred to as either "constants" or "constant fields" a lot. (Which explains the meaning of the C# `const` keyword) – Broots Waymb Oct 28 '15 at 17:41
  • But how can an immutable variable be useful? Or what are its advantages? Don't you think it may be better to use mutable variables? @Lee – Alex Jones Oct 28 '15 at 17:49
  • I will try to incorporate that into an answer. I believe I know what you're asking now. – Broots Waymb Oct 28 '15 at 17:50

2 Answers2

4

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"?

If you consider class members "variables" then Yes. If not, then No. C++ has the concept of "constant" local variables but C# does not.

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. One is readonly, which can be set at run-time as part of object construction (or static construction for a static redaonly field).

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

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. The language actually creates a NEW string object that myString will point to. (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). 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:

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.

Community
  • 1
  • 1
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
  • 1
    There is also the `readonly` key word. The difference is explained [here](http://stackoverflow.com/questions/755685/static-readonly-vs-const) – Broots Waymb Oct 28 '15 at 18:12
  • so is it just constants or constants variables? but thank you for the answer it clears a lot of things in my mind now. – Alex Jones Oct 28 '15 at 22:01
  • Most people would just say "constants". A "constant variable" doesn't really make sense (it is a oxymoron). Constant = cannot change. Variable = Changing. So a constant variable = a changing thing that can't actually change. – Broots Waymb Oct 29 '15 at 13:35
  • But I thought variable is a place for storing a value. which can be read back or changed. – Alex Jones Oct 29 '15 at 22:26
  • It is. Which is why saying a "immutable variable" doesn't really make sense, although it is technically a real thing. Since immutable means it cannot be changed. It's just confusing terminology. Adding `const` to a variable just means you cannot change it. If I were "modify" your above comment to describe "constant variables": `A constant is a place for storing a value, which can be read back`. This is why we don't usually call them "immutable variables" or "constant variables" and prefer "constants" or "constant fields" instead. – Broots Waymb Oct 30 '15 at 13:33
  • But what about Static Variable? because they also have the same meaning. right? @DangerZone – Alex Jones Nov 10 '15 at 11:01
  • @AlexJones, yes and no. Essentially, a constant will behave the same as a static variable (no more than 1 needed per instance) with the added condition it cannot change, but you can have static variables that are not at all constant. – Broots Waymb Nov 10 '15 at 14:30