473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Mutable struct

What is a mutable struct?

regards
Kjetil Kristoffer Solberg
Nov 16 '05 #1
12 6334
Kjetil Kristoffer Solberg <kk*@pride.no> wrote:
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; }
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kjetil Kristoffer Solberg <kk*@pride.no> wrote:
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; }
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
"Kjetil Kristoffer Solberg" <kk*@pride.no> wrote:
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.
Nov 16 '05 #4
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" <kk*@pride.no> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kjetil Kristoffer Solberg <kk*@pride.no> wrote:
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; }
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #5
Dennis Myrén <de****@oslokb.no> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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; }
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Dennis Myrén <de****@oslokb.no> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Thank you mr Skeet.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Dennis Myrén <de****@oslokb.no> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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; }
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Thank you mr Skeet.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Dennis Myrén <de****@oslokb.no> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
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

"Kjetil Kristoffer Solberg" <kk*@pride.no> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl... What is a mutable struct?

regards
Kjetil Kristoffer Solberg

Nov 16 '05 #12
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

"Kjetil Kristoffer Solberg" <kk*@pride.no> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl... What is a mutable struct?

regards
Kjetil Kristoffer Solberg

Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
18
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities?...
12
by: Water Cooler v2 | last post by:
Are JavaScript strings mutable? How're they implemented - 1. char arrays 2. linked lists of char arrays 3. another data structure I see that the + operator is overloaded for the string class...
3
by: Mythran | last post by:
http://msdn2.microsoft.com/en-US/library/ms229057(VS.80).aspx * Do not assign instances of mutable types to read-only fields. I would have to disagree with this "Field Design" guidelines...to...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
3
by: Sambo | last post by:
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near...
2
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on...
24
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.