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

readonly issue

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?
}
Oct 6 '08 #1
5 1090
puzzlecracker <ir*********@gmail.comwrote:
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".

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 6 '08 #2
On Mon, 06 Oct 2008 12:05:39 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
When I create a reference type as readonly, can I modify its internals
but not resassing?
Just try to compile it and see yourself.
Oct 6 '08 #3
On Oct 6, 4:29*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
puzzlecracker <ironsel2...@gmail.comwrote:
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".

--
Jon Skeet - <sk...@pobox.com>
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?
Oct 6 '08 #4
puzzlecracker <ir*********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 6 '08 #5
ozbear <oz****@bigpond.comwrote:
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<stringnames = 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

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jack | last post by:
Hi, I have a asp page where a checkbox, when clicked will disable all text boxes associated with the page. The following is a part of a code for the checkbox with its name and readonly...
13
by: My4thPersonality | last post by:
I am reading something about the details of C#, and I came acros the statements readonly and const. I do not understand, it seems to be the same, what's the difference? Here is the text were it...
11
by: Christof Nordiek | last post by:
Hi all, Can there be a performance difference, if i use readonly fields instead of constants? e.g.: const int n = 100; vs. static readonly int = 100;
8
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if...
2
by: Patti | last post by:
I am building an application using VB.NET 1.1 and I have an issue with Font properties in my Radio Button Lists and Check Box Lists. My issue is that the Font Names and Font Size for these...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hello misters I am having troubles impementing the readonly attribute properly. In case the readonly attribute is set on the server side, I added an attribute:...
4
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a;...
2
by: Andrus | last post by:
If focused control is ReadOnly, menu shortcut key is ignored. To reproduce, run code below, press Ctrl+E. Note that message box does not appear. This issue blocks Ctrl+E usage in application....
0
by: Nergock | last post by:
Have anyone run into this difference with SQL Server 2005 and the SqlDataTable Load method? In SQL Server 2000, when I create a table using the SqlDataTable.Load method passing it a...
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
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
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...
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...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.