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

readonly behavior for structs

Hi All,

I was surprised to see the readonly behavior differ with respect to
properties and methods.

struct Foo
{
int state;

public int State
{
get { return state; }
set { state = value; }
}

public void SetState(int x)
{
state = x;
}
}

Now if I do something like
class Temp
{
readonly Foo f = new Foo();

public void SomeMethod()
{
f.State = 5; // Doesn't compile, ok
f.SetState(5); // Compiles !!!
}
}

The funny thing is although the method call succeeds, the state
variable in f doesn't change. Using ildasm, I can see that indeed, the
methodcall was made on a copy of f, like
Foo f2 = f;
f2.SetState(5);

One, I personally think that's misleading, it obviously looks like it
is calling the method on f. Two, why doesn't the same thing happen when
the State property was assigned to? After all, assignment to a property
ultimately translates into a set method call. Ideally, the compiler
should report error on both statements.
What do you think?

Regards
Senthil

Nov 16 '05 #1
4 2478
sadhu <se**********@wdevs.com> wrote:
I was surprised to see the readonly behavior differ with respect to
properties and methods.


<snip>

It surprises me, too. It *is* in the language spec - buried fairly
deeply, admittedly, in the member lookup section. Basically, when
accessing a readonly field outside a constructor (or static constructor
for a static field) the result of the member lookup is the value of the
field, not the field itself.

(Section 14.5.4 of the ECMA spec, parts 8 and 22.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
One, I personally think that's misleading, it obviously looks like it
is calling the method on f. Two, why doesn't the same thing happen when
the State property was assigned to? After all, assignment to a property
ultimately translates into a set method call. Ideally, the compiler
should report error on both statements.
What do you think?


Setting a property is almost guaranteed to be modifying the object.
Invoking a regular method is not, and the compiler doesn't know how
SetState will affect the value.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
sadhu <se**********@wdevs.com> wrote:
I was surprised to see the readonly behavior differ with respect to
properties and methods.


<snip>

It surprises me, too. It *is* in the language spec - buried fairly
deeply, admittedly, in the member lookup section. Basically, when
accessing a readonly field outside a constructor (or static constructor
for a static field) the result of the member lookup is the value of the
field, not the field itself.

(Section 14.5.4 of the ECMA spec, parts 8 and 22.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Thanks for the explanation.

If I understood the spec correctly, 14.5.4.25 says this.f will result
in a value, not a variable. And 14.3.1, paragraph 5 (dealing with
assignment) says that if the instance associated with the property
happens to be a value and not a variable, compile time error occurs. So
the assignment to this.f.State fails to compile.
There is no such restriction for methods, so f.SetState() compiles.

Nov 16 '05 #5

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

Similar topics

4
by: kaeli | last post by:
All, I have need of a readonly select element that looks and acts disabled to the user. The problem with the disabled attribute is that the value isn't passed to the handler, so I'm using...
4
by: kaeli | last post by:
Hi all, I have form elements that I want to not be editable. They should, however, be passed on submission. AFAIK, then, they need to be readonly. (intranet app where elments are created...
5
by: pv_kannan | last post by:
Hello, I am stuck with a problem trying to mimic the behavior of a Windows Forms Readonly textbox in HMTL/ASP.NET. Basically, I would like the text to be readonly and overflow the textbox...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
1
by: Danielb | last post by:
I need to create a read-only copy of Object X at run time, I know how to find the type of Object X (using GetType) but how do I go about creating a readonly copy of X? What I want to do is this:...
1
by: sadhu | last post by:
Hi All, I was surprised to see the readonly behavior differ with respect to properties and methods. struct Foo { int state; public int State
5
by: fred | last post by:
With a Class is there any difference between a readonly property and function besides having to use Get/End Get. Are there any performance/resource advantages for either. Thanks Fred
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.