Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why C# does not provide internal helper for passing property as reference?

Tags:

c#

This is issue about LANGUAGE DESIGN.

Please do not answer to the question until you read entire post! Thank you.

With all helpers existing in C# (like lambdas, or automatic properties) it is very odd for me that I cannot pass property by a reference. Let's say I would like to do that:

foo(ref my_class.prop);

I get error so I write instead:

{
   var tmp = my_class.prop;
   foo(tmp);
   my_class.prop = tmp;
}

And now it works. But please notice two things:

  1. it is general template, I didn't put anywhere type, only "var", so it applies for all types and number of properties I have to pass

  2. I have to do it over and over again, with no benefit -- it is mechanical work

The existing problem actually kills such useful functions as Swap. Swap is normally 3 lines long, but since it takes 2 references, calling it takes 5 lines. Of course it is nonsense and I simply write "swap" by hand each time I would like to call it. But this shows C# prevents reusable code, bad.

THE QUESTION

So -- what bad could happen if compiler automatically create temporary variables (as I do by hand), call the function, and assign the values back to properties? Is this any danger in it? I don't see it so I am curious what do you think why the design of this issue looks like it looks now.

Cheers,

EDIT As 280Z28 gave great examples for beating idea of automatically wrapping ref for properties I still think wrapping properties with temporary variables would be useful. Maybe something like this:

Swap(inout my_class.prop1,inout my_class.prop2);

Otherwise no real Swap for C# :-(

like image 253
greenoldman Avatar asked Feb 02 '10 07:02

greenoldman


People also ask

Why C is the best language?

It is fast The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

Why C function is used?

There are the following advantages of C functions. By using functions, we can avoid rewriting same logic/code again and again in a program. We can call C functions any number of times in a program and from any place in a program. We can track a large C program easily when it is divided into multiple functions.

Why should you learn C?

It helps you understand how a computer works By learning C, you can be able to understand and visualize the inner workings of computer systems. This can include aspects like allocation and memory management along with their architecture and the overall concepts that drive programming.


3 Answers

There are a lot of assumptions you can make about the meaning and behavior of a ref parameter. For example,

Case 1:

int x;
Interlocked.Increment(ref x);

If you could pass a property by ref to this method, the call would be the same but it would completely defeat the semantics of the method.

Case 2:

void WaitForCompletion(ref bool trigger)
{
    while (!trigger)
        Thread.Sleep(1000);
}

Summary: A by-ref parameter passes the address of a memory location to the function. An implementation creating a temporary variable in order to "pass a property by reference" would be semantically equivalent to passing by value, which is precisely the behavior that you're disallowing when you make the parameter a ref one.

like image 92
Sam Harwell Avatar answered Oct 29 '22 03:10

Sam Harwell


Your proposal is called "copy in - copy out" reference semantics. Copy-in-copy-out semantics are subtly different from what we might call "ref to variable" semantics; different enough to be confusing and wrong in many situations. Others have already given you some examples; there are plenty more. For example:

void M() { F(ref this.p); }
void F(ref int x) { x = 123; B(); }
void B() { Console.WriteLine(this.p); }

If "this.p" is a property, with your proposal, this prints the old value of the property. If it is a field then it prints the new value.

Now imagine that you refactor a field to be a property. In the real language, that causes errors if you were passing a field by ref; the problem is brought to your attention. With your proposal, there is no error; instead, behaviour changes silently and subtly. That makes for bugs.

Consistency is important in C#, particularly in parts of the language that people find confusing, like reference semantics. I would want either references to always be copy-in-copy-out or never copy-in-copy-out. Doing it one way sometimes and another way other times seems like really bad design for C#, a language which values consistency over brevity.

like image 23
Eric Lippert Avatar answered Oct 29 '22 05:10

Eric Lippert


Because a property is a method. It is a language construct responding to a pattern of encapsulating the setting and retrieval of a private field through a set of methods. It is functionally equivalent to this:

class Foo
{
    private int _bar;
    public int GetBar( ) { return _bar; }
    public void SetBar( ) { _bar = value; }
}
like image 20
Ed S. Avatar answered Oct 29 '22 04:10

Ed S.