Re: List<> of struct with property. Cannot change value of property. why?

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Tue, 15 May 2007 14:25:19 -0700, Zytan <zytanlithium@xxxxxxxxx> wrote:

I think we are speaking about the same thing, so perhaps you
misunderstand my idea. I just thought if C# had "const" like C++
does, then it could go one step further, and make use of it to show a
warning that you are calling a non-const (i.e. potentially modifying)
method on a temporary, which seems pointless.

I think I cover this adequately in the article I just posted, but just for completeness...

The problem (well, "a problem" anyway) is that lots of methods are essentially "const" even though no one's bothered to mark them as such. For that matter, lots of methods are essentially "const" even though they can't be marked as such, because they might call some other code that is essentially "const" but which isn't marked as such.

I suppose you could work it the other way around, claiming that code is "const" unless otherwise marked (say, introduce an "unconst" keyword you have to use any time you want a method to be able to change things). But then you'd have the language assuming a bunch of external functions are const even though they are not (or if the language doesn't do that, you wind up with the previous problem *plus* an inconsistency in how functions are treated).

No, I wouldn't want things to change depending if it had const-ness or
not! It could just be used for a helpful warning, that's all: Why
call a method that changes the class when the class is a temporary,
and is thrown away before you can even see or use it?

Well, a *class* _isn't_ temporary. If you get a reference from a List<>, then yes you get a copy of the reference, just like you get a copy of a value type. But the reference refers to a single instance, and if you change that instance, that change is reflected whether you look at the copied reference you're using, or the original reference stored in the List<>.

Pete
.



Relevant Pages