473,385 Members | 1,470 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,385 software developers and data experts.

Mutable Types and Read-Only Fields

http://msdn2.microsoft.com/en-US/lib...57(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;
public SomeClass() { mCollection = new NameValueCollection(); }
public NameValueCollection Collection {
get { return mCollection; }
}
public int CollectionCount()
{
return mCollection.Count;
}
}

For the above (granted, there may be some errors as this is just off top of
my head), the CollectionCount method requires that the mCollection field be
set. I expose the collection to the callee using a read-only property. The
property returns the mutable type just as I want. In a scenerio for my
example, the callee is allowed to change the internal collection values
(add/remove items to a list). But, if the user was allowed to modify the
actual reference, they could set it to null which would cause other areas of
the class (the mCollection.Count) to throw NullReferenceException's.
Therefore, am I not correct that in this instance, the mutable type being
returned by a read-only field is an exception to the rule?

Thanks,
Mythran

Apr 20 '06 #1
3 2132
Why does it need to be read only? Why not just have a get accessor? I
don't think there's any reason to this rule other than the fact that
"readonly mutable type" is misleading. Yes you can't change the
reference but readonly also should imply that you can't change the
value (and thus change things like the return from GetHashCode(), etc).

Apr 20 '06 #2
Hi,

For the above (granted, there may be some errors as this is just off top
of my head), the CollectionCount method requires that the mCollection
field be set. I expose the collection to the callee using a read-only
property.
You expose it using a get property, using this only assure that the
reference holded in the class will not change, on other words, if you are
holding a reference to instance A the callee can not make change it to
instance B. IT DOES NOT assure that the instance itself cannot change its
state. If you want this the referenced class hasve to be inmutable ( like
string).
The property returns the mutable type just as I want. In a scenerio for
my example, the callee is allowed to change the internal collection values
(add/remove items to a list). But, if the user was allowed to modify the
actual reference, they could set it to null which would cause other areas
of the class (the mCollection.Count) to throw NullReferenceException's.
Therefore, am I not correct that in this instance, the mutable type being
returned by a read-only field is an exception to the rule?


I will try to explain, but the language can be confused, feel free to
comment back if further clarification is needed.

A readonly field and a read-only property ( which only define a get ) has
nothing in common. They are two completely different concepts.

a readonly field is declared using the readonly reserved word, this has two
implications:
1- a value should be assign in the spot or in a constructor
2- after it's assigned you cannot change it.

If you assign a primitive type you have effectively blocked that value :

readonly int i=5;

has the effect that i will ALWAYS be 5.

A similar thing happens with a struct:

public struct S
{
public int i;
}

class C
{
readonly S s = new S();

void method1()
{
s.i=4; //error
}

}
Now if the readonly is a referenced type, all the above change.

What you cannot change is the referenced instance, the member of the
instance can be changed at will:

public class S
{
public int i;
}

class C
{
readonly S s = new S();

void method1()
{
s.i=4; //good now
s = new S(); //error
}

}
hope this clarify it.
Let me know if not.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Apr 20 '06 #3
On 2006-04-20, Mythran <ki********@hotmail.comREMOVETRAIL> wrote:
http://msdn2.microsoft.com/en-US/lib...57(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;
public SomeClass() { mCollection = new NameValueCollection(); }
public NameValueCollection Collection {
get { return mCollection; }
}
public int CollectionCount()
{
return mCollection.Count;
}
}
The guidelines you reference are distinguishing between fields and
properties. In the above code, the property is readonly, while the
field is not, which is exactly what the guidelines suggest doing.

For the above (granted, there may be some errors as this is just off top of
my head), the CollectionCount method requires that the mCollection field be
set. I expose the collection to the callee using a read-only property. The
property returns the mutable type just as I want. In a scenerio for my
example, the callee is allowed to change the internal collection values
(add/remove items to a list). But, if the user was allowed to modify the
actual reference, they could set it to null which would cause other areas of
the class (the mCollection.Count) to throw NullReferenceException's.
Therefore, am I not correct that in this instance, the mutable type being
returned by a read-only field is an exception to the rule?

Thanks,
Mythran

Apr 22 '06 #4

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...
49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
3
by: Ingo Nolden | last post by:
Hi, I try to use const where ever appropriate. In a collection class I am counting the iterators that are out. The counter decrements when an iterator leaves scope or is 'Dispose( )'d While...
6
by: christopher diggins | last post by:
I wrote a dynamic matrix class similar to the one described in TCPL 3rd Edition. Rather than define two separate iterators for const and non-const scenarios I decided to be a lazy bastard and only...
13
by: Suresh Jeevanandam | last post by:
# I am new to python. In python all numbers are immutable. This means there is one object ( a region in the memory ) created every time we do an numeric operation. I hope there should have been...
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...
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.
5
by: Andreas Beyer | last post by:
There has been quite some traffic about mutable and immutable data types on this list. I understand the issues related to mutable numeric data types. However, in my special case I don't see a...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.