What is a mutable struct?
regards
Kjetil Kristoffer Solberg 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
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
"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.
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
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
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
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
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
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
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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?...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |