473,520 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mutable struct

What is a mutable struct?

regards
Kjetil Kristoffer Solberg
Nov 16 '05 #1
12 6344
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
7378
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 interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some...
50
6290
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the...
18
2567
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? http://david.tribble.com/text/cdiffs.htm#C99-cpp-keyword http://www.inf.uni-konstanz.de/~kuehl/c++-faq/const-correctness.html#faq-18.13 Regards, Markus
12
3718
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 and hence it is possible to do: txt += "foo bar";
3
2141
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 an extent. Example: public class SomeClass { private NameValueCollection mCollection;
12
3128
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
1446
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 the end ). class Cgroup_info: group_name = "" count = "0" #last time checked and processed/retrieved first = "0" last = "" retrieval_type =...
2
4251
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 whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for...
24
2474
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 finally, and painfully, fade away than another one starts up. I suggest that Python should raise warnings.RuntimeWarning (or similar?) when a function...
0
7201
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7602
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7559
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5740
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3282
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3279
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.