Is it possible to use the same method with and without ref?

I have a method that takes a ref parameter. Is there a way to use the same method (without overloading) without passing the parameter as ref?

+3
source share
4 answers

Is there a way to use the same method (without overloading) without passing the parameter as ref?

Yes, but this feature probably won't help you.

In C # 4, it will not be necessary to use "ref" to invoke a COM object that accepts an optional ref parameter.

, COM-, VB6. VB6 ref. # , , # :

object missing = Type.Missing;
object abc = 10;
foo.Blah(ref missing, ref missing, ref missing, ref abc, ref missing);

. # 4

foo.Blah( whatever : 10 );

ref whatnot .

COM- # 4. "ref", .

+2

. ref , (, , ).

"ref-ness" , , , , , .

, , .

+10

As far as I know, if the method signature indicates ref, you need to pass the parameter as ref. Thus, the compiler knows that the value needs to be initialized before passing, and not passing something as outside, where it is initialized / assigned inside the method.

+3
source

No ... could you presumably make a copy of the value you want to pass as a parameter so that ref does not change your original value?

+1
source

Source: https://habr.com/ru/post/1732134/


All Articles