K
Kjetil Kristoffer Solberg
What is a mutable struct?
regards
Kjetil Kristoffer Solberg
regards
Kjetil Kristoffer Solberg
Kjetil Kristoffer Solberg said:What is a mutable struct?
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?
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
Jon Skeet said:struct Mutable
{
int value;
public Immutable(int value)
{
this.value = value;
}
public int Value
{
get { return value; }
set { this.value = value; }
}
}
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
struct Immutable
{
public Immutable(int value)
{
this.value = value;
}
public int Value
{
get { return value; }
}
}