Immutable Datatypes

LeonR

Well-known member
Joined
Nov 2, 2006
Messages
139
Location
UK
Programming Experience
10+
I was after some opinions and views in regards to immutable and mutable data types.

For example, I understand that if you were to define a string, that is creating and storing a string of char's in memory and then pointing a reference to that location? (indirect addressing?)

That makes sense, but if you then created another string with the same value, does that point to the same memory location?
This brings me on to ask, what happens when you change one of the variable values? Does it create and point to a new memory location?

I was under the impression that to do a comparison in the form of 'if stringA=stringB' was bad practice? (but if it was to create a new memory location when you change a variables value, why would this matter?)

Also, is an Integer treated the same way? If it was, would 'if intA=intB' be practice also?

How are you meant to be able to know the best way to determine comparisons?


Obviously my concerns may not be a problem within .net.


Thanks,
Leon
 
These are two different topics that you seems to have mixed up.

Data types are divided into value types and reference types. Value types represent simple values, assignments is 'copy by value' (new value). Reference types represent more complex objects, assignments is "copy by reference" (same object).

Immutable means once something is created it can't be changed. An example of this is String class, it has no properties or methods that will change the current string object. An example of a mutable value type is the Point Structure, it has X and Y properties that allow modification of the current Point value - this is just a convenience and has no practical meaning since a value type has no reference equality. Most value types are immutable, while most reference types are mutable, though.

Reference equality can be compared using the Is operator, while value equality can be compared with = operator. That for example two strings have same value equality doesn't mean they have reference equality, it usually makes sense to compare strings as values.
 
Thanks John!

So in that case, it is ok to compare strings using the = operator? (.net takes care of this and uses the value automatically?)
 
Yes, if you want to compare if two strings represent same string value use the = comparison operator.
 
Thanks :)

The reason I brought this up was because an expereinced Java developer friend pointed out using '=' operand for strings is not always good practice because strings are immutable, which prompted me to check this with the .net community!
I'm one of those people who once finds out about something important like this I have to try and fully understand it, or I will not be happy until I have figured it out!

Thanks again!
 
If you wanted to check reference equality = operator would obviously be wrong, so you need to know the difference and use the correct operators. Some reference types, like String class, support both kinds of comparisons and compiler wouldn't know and notify you if you used the wrong one.

For equality comparisons there is also the dynamic Equals instance method, you can read about that in subtopics here: Object.Equals Method (System)
IEquatable and IComparable generic interfaces are also related to this if you want further study.
 
Back
Top