Mutable struct

  • Thread starter Thread starter Kjetil Kristoffer Solberg
  • Start date Start date
Kjetil Kristoffer Solberg said:
What is a mutable struct?

One which allows its contents to be changed. Here are two structs, one
immutable and one mutable:

struct Immutable
{
int value;

public Immutable(int value)
{
this.value = value;
}

public int Value
{
get { return value; }
}
}

struct Mutable
{
int value;

public Immutable(int value)
{
this.value = value;
}

public int Value
{
get { return value; }
set { this.value = value; }
}
}
 
Ok, but in your first example the Immutable structs private int value is set
by the Immutable method. Is it not mutable as well
then? or is a struct mutable only if a value changes through a property?

regards
Kjetil Kristoffer Solberg
 
The Immutable method is the constructor of the struct.
Meaning that once it has been set, it cannot be changed again.

Immutable immutable = new Immutable(/* value */ 10);
// Now I want to change it but I cant, it throws an Exception.
immutable.Value = 11; // Raises Exception


Mutable mutable= new Mutable(/* value */ 10);
// Now I want to change it AND I can.
mutable.Value = 11; // DO NOT Raise Exception


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Kjetil Kristoffer Solberg said:
Ok, but in your first example the Immutable structs private int value is set
by the Immutable method. Is it not mutable as well
then? or is a struct mutable only if a value changes through a property?

regards
Kjetil Kristoffer Solberg
 
Kjetil Kristoffer Solberg said:
Ok, but in your first example the Immutable structs
private int value is set by the Immutable method.
Is it not mutable as well then? or is a struct mutable
only if a value changes through a property?

The Immutable "method" is the constructor - you can only call it once,
when you create an instance of the struct.

P.
 
Dennis Myrén said:
The Immutable method is the constructor of the struct.
Meaning that once it has been set, it cannot be changed again.
Correct.

Immutable immutable = new Immutable(/* value */ 10);
// Now I want to change it but I cant, it throws an Exception.
immutable.Value = 11; // Raises Exception

No, it doesn't raise an exception - it just doesn't compile in the
first place.
 
Jon Skeet said:
struct Mutable
{
int value;

public Immutable(int value)
{
this.value = value;
}

public int Value
{
get { return value; }
set { this.value = value; }
}
}

Apologies - this should, of course, be:

struct Mutable
{
int value;

public Mutable(int value)
{
this.value = value;
}

public int Value
{
get { return value; }
set { this.value = value; }
}
}
 
Thank you mr Skeet.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
The Immutable method is the constructor of the struct.
Meaning that once it has been set, it cannot be changed again.
Correct.

Immutable immutable = new Immutable(/* value */ 10);
// Now I want to change it but I cant, it throws an Exception.
immutable.Value = 11; // Raises Exception

No, it doesn't raise an exception - it just doesn't compile in the
first place.
 
Kjetil,
One thing I like to do is to make the value field in Immutable readonly to
re-emphasize the fact that it is immutable.

Borrowing Jon's example:
struct Immutable
{

readonly int value;
public Immutable(int value)
{
this.value = value;
}

public int Value
{
get { return value; }
}
}

The readonly on value means that it can only be set in the constructor,
every other method you attempt to set it in will receive a compile error.

Hope this helps
Jay
 
Back
Top