Is there a technical reason for requesting outlier and ref keywords for the caller?

When calling a method with the ref or out parameter, you must specify the appropriate keyword when calling the method. I understand this in terms of style and code (as explained here here ), but I'm curious if there is a technical need for keywords that will be indicated in the caller.

For instance:

 static void Main() { int y = 0; Increment(ref y); // Is there any technical reason to include ref here? } static void Increment(ref int x) { x++; } 
+6
source share
4 answers

If you ask if the language could be designed so that it was not needed on the call site, the answer is yes. There is no particular reason why they could not be forgotten. The compiler has all the necessary information from the metadata, so it can do the right conversion.

However, this would make these two overloads impossible:

 public void DoSomething(int x); public void DoSomething(ref int x); 

The compiler cannot fix this.

Although ref and out could be made optional, in this case these overloads will be allowed. And the compiler can either accept the default value (i.e. Not ref), or throw an ambiguity error and indicate which one you really need.

All that said, I like to specify ref and out on the call site. He tells me that the parameter can be changed. Having worked in Pascal for many years, where the var parameter is passed in the same way as the value parameter is passed (the syntax on the call site is the same), I prefer the specifics of C # in this regard.

+11
source

The only technical reason I can think of is overload resolution: you could

 static void Increment(ref int x) 

and

 static void Increment(int x) 

This is allowed ; without ref in the call, the compiler cannot tell them apart.

+12
source

Another reason a modifier is required is to change the parameter to ref or out violation. If ref / out could be inferred, then the evil programmer changing the parameter from value to link will not be detected by clients compiling against a new signature. If the client called the method

 public int Increment(int x) { return x + 1; } 

using

 int result = Increment(x); 

Suppose that the evil developer decided to change the implementation, instead changing the value passed by reference, and will return an error code if, say, the increment led to overflow:

 public int Increment(ref int x) { x = x + 1; if(x == int.MinValue) // overflow return -1; else return 0; } 

Then the client building against the signature will not receive a compilation error, but it will almost certainly break the calling application.

+9
source

The compiler must know the parameters of the method, which it interprets when it creates the IL. One of the reasons is overload, such as:

 public void (ref int x) {} public void (int x) {} 

Another reason: out explicitly allow you to use the pass-by-value parameter to use the interpreted value outside the method. ref will give a pointer to the parameter, specifying any new value from the method in the same memory cell

-1
source

All Articles