readonly issue

P

puzzlecracker

When I create a reference type as readonly, can I modify its internals
but not resassing?


class A
{
private int _x;
public int X
{
get; set;
}
}

public static void Main(string [] args){

readonly A a=new A();;
A b=new A();
a.X=5; //is this allowed?
a=b; // is this allowed?
}
 
J

Jon Skeet [C# MVP]

puzzlecracker said:
When I create a reference type as readonly

Careful with the terminology - it's not the type which is being marked
as readonly, but the variable.
can I modify its internals but not resassing?

Exactly. There's no way of saying "this is an immutable instance".
 
P

puzzlecracker

Careful with the terminology - it's not the type which is being marked
as readonly, but the variable.


Exactly. There's no way of saying "this is an immutable instance".

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

isn't string immutable? Or Csharp doesn't have the same concept of
immutability as C++ (const T t; would make t constant, hence no
modifications are allowed to internals)? I suppose readonly SomeType
t=new SomeType() is equivalent to SomeTime *const t in C++. Right?
 
J

Jon Skeet [C# MVP]

puzzlecracker said:
isn't string immutable?

Yes, but we happen to know that - there's no way of telling either the
CLR or the compiler that a particular type is immutable.
Or Csharp doesn't have the same concept of
immutability as C++ (const T t; would make t constant, hence no
modifications are allowed to internals)?
Exactly.

I suppose readonly SomeType
t=new SomeType() is equivalent to SomeTime *const t in C++. Right?

I suspect so, but my C++ is very rusty, so I wouldn't swear to it.
 
O

ozbear

Yes, but we happen to know that - there's no way of telling either the
CLR or the compiler that a particular type is immutable.

Which is a shame. I had written what I thought was an
immutable class but missed something which rendered it
mutable. It would be nice if there were an attribute
or other decoration that one could put on a class
definition that stated it was (supposed to be) immutable.
Enforcement of that decoration would, I believe, not be difficult for
the compiler. All fields readonly so they could only
be set in a constructor would be adequate.

Oz
 
J

Jon Skeet [C# MVP]

ozbear said:
Which is a shame. I had written what I thought was an
immutable class but missed something which rendered it
mutable. It would be nice if there were an attribute
or other decoration that one could put on a class
definition that stated it was (supposed to be) immutable.
Enforcement of that decoration would, I believe, not be difficult for
the compiler. All fields readonly so they could only
be set in a constructor would be adequate.

I agree that it's a shame that there isn't more support for
immutability - but I disagree that it's as simple as making all the
fields readonly:

public class SupposedlyImmutable
{
private readonly List<string> names = new List<string>();

public void AddName(string name)
{
names.Add(name);
}
}

Eric Lippert has a great series of posts about immutability:

blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx
 
O

ozbear

I agree that it's a shame that there isn't more support for
immutability - but I disagree that it's as simple as making all the
fields readonly:

public class SupposedlyImmutable
{
private readonly List<string> names = new List<string>();

public void AddName(string name)
{
names.Add(name);
}
}

Eric Lippert has a great series of posts about immutability:

blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx

Yes, indeed some other restrictions would also need to be
enforced, perhaps by cascading the immutablity to all subobjects
rendering the names.Add statement illegal outside of a
constructor.

Thanks for the link.

Oz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top