This is a question about LANGUAGE DESIGN.
Please do not answer the question until you read the whole post! Thanks.
With all the helpers existing in C # (like lambdas or automatic properties), it is very strange for me that I cannot pass a property by reference. Let's say I would like to do this:
foo(ref my_class.prop);
I get an error, so instead I write:
{ var tmp = my_class.prop; foo(tmp); my_class.prop = tmp; }
And now it works. But please pay attention to two things:
this is a general template, I didn’t add a type anywhere, only “var”, so it applies to all types and the number of properties that I need to pass
I need to do it again and again, to no avail - this is mechanical work
The existing problem actually kills useful features like Swap. Swap usually has a length of 3 lines, but since it takes 2 links, the call takes 5 lines. Of course, this is nonsense, and I just write "swap" manually every time I would like to name it. But this shows that C # prevents multiple code, badly.
QUESTION
So - what could be bad if the compiler automatically creates temporary variables (as I do manually), call the function and assign values to the properties? Is this some kind of danger in this? I don’t see this, so I wonder how you think why the design of this problem looks the way it looks now.
Greetings
EDIT . Since the 280Z28 gave great examples for beating the idea of automatically wrapping property references, I still find it useful to use packaging properties with temporary variables. Maybe something like this:
Swap(inout my_class.prop1,inout my_class.prop2);
Otherwise, there is no real Swap for C #: - (
design c #
greenoldman
source share