What is a mutable struct?
regards
Kjetil Kristoffer Solberg 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
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 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...
|
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...
|
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
|
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";
|
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;
| |
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 the end ).
class Cgroup_info:
group_name = ""
count = "0" #last time checked and processed/retrieved
first = "0"
last = ""
retrieval_type =...
|
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...
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |