user10841478
user10841478

Reputation:

how ref and out are different at run time?

in many of the articles they had given that ref and out out works in the same way.even at compile time recognises the both in same way but they are differed at run time(CLR).can anyone explain how it is differed? and functionality of out?

i tried the same in this code

public static void Main()
    {
        int par = 7;
        Program x= new Program();
        x.RefMethod(out par);
        Console.WriteLine(par);
        x.RefMethod1(ref par);
        Console.WriteLine(par);

        Console.ReadLine();
    }
    public void RefMethod(out int i)
    {
        i = 10;

    }
    public void RefMethod1(ref int i)
    {
        i = 20;
    }

ILDASM for refmethod:

method public hidebysig instance void  RefMethod([out] int32& i) cil managed
{
  // Code size       6 (0x6)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.1
  IL_0002:  ldc.i4.s   10
  IL_0004:  stind.i4
  IL_0005:  ret
} // end of method Program::RefMethod

ILDASM for the Refmethod1:

method public hidebysig instance void  RefMethod1(int32& i) cil managed
{
  // Code size       6 (0x6)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.1
  IL_0002:  ldc.i4.s   20
  IL_0004:  stind.i4
  IL_0005:  ret
} // end of method Program::RefMethod1

the above clearly shows that both are differed at one point i.e. at that method initialising.please can anyone help me out with my question?

Please any one answer the question with a clarity. I still aint got the answer :(

Upvotes: 1

Views: 643

Answers (2)

Brian
Brian

Reputation: 25834

Note: This answer references misleading MSDN documentation, which has since been corrected.


Gauravsa's answer is correct. There is no run-time difference.

You mention https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/, which states:

Both ref and out are treated differently at run time and they are treated the same at compile time, so methods cannot be overloaded if one method takes an argument as ref and the other takes an argument as an out.

The author of that article likely read https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier , which states:

Although the in, ref, and out keywords cause different run-time behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref or in argument and the other takes an out argument.

Personally, I agree with jmoreno, who argues that the MSDN should have used the phrase, "require different behavior" instead of "cause difference behavior."

It's not surprising that you're seeing such an explanation in several places. Probably many authors who try to use Microsoft's documentation as their primary source of information are providing the same misleading explanation.

Upvotes: 0

Gauravsa
Gauravsa

Reputation: 6524

The difference between ref and out is not in the CLR but in the C# language itself. Ref and Out differs in a way is that the "compiler requires different behaviour" for each of them. Method overloading also isnt allowed at compile time.

As you can see from the code, the only change is address reference specifically this line (if you put value 10, this will up as 10).

IL_0002:  ldc.i4.s   20

Upvotes: 1

Related Questions