1

Can anyone tell me which one is lighter between ref and out. I know the exact difference between ref and out keyword and usage as well.

In my case I can use both ref and out, but wondering the lighter one.

If some one needs to refer please go here and let me know the lighter ones.

Thanks in advance..

Community
  • 1
  • 1
DotNet
  • 37
  • 3

4 Answers4

12

The only difference is a compiler hint.

... out ...

public static void TestOut(out int test)
{
    test = 1;
}

.method public hidebysig static void  TestOut([out] int32& test) cil managed
{
  // Code size       4 (0x4)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.1
  IL_0002:  stind.i4
  IL_0003:  ret
} // end of method Program::TestOut

... ref ...

public static void TestRef(ref int test)
{
    test = 1;
}

.method public hidebysig static void  TestRef(int32& test) cil managed
{
  // Code size       4 (0x4)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.1
  IL_0002:  stind.i4
  IL_0003:  ret
} // end of method Program::TestRef

... out and ref are effectively the same. The only real difference being that out tells the compiler to expect the value to be set before the method is returned. You could send a value to a function that has an out flag but again the compiler will treat it as an unassigned variable. The runtime doesn't really care. Both will be created as a pointer to the variable. You are best to use the keyword that describes the functionality you expect with your function. Any optimization that "may" take place in the JITer below this will have near 0 impact on the application.

Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
3

They will most likely be the same at the low-level. If there is a performance difference, it's probably negligible and there are better places to optimize.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • I know there are plenty of places to optimise, but wanted to know the exact difference in comparision to weight.I have some observable collections and passing by refernce by using either of the above keywords, so wanted optimize them.may be this is the start for optimization :)thanks for the reply by the way. :) – DotNet Nov 21 '12 at 21:31
3

This is a micro-optimization.

Actually, if you have a look at the IL code that is being generated for both options, you'll see no difference. Except for the compiler hint that Matthew Whited indicated:

.method private hidebysig static void  Out([out] valuetype [mscorlib]System.DateTime& d) cil managed

.method private hidebysig static void  Ref(valuetype [mscorlib]System.DateTime& d) cil managed

I'd just suggest the option that is semantically the most correct one for the situation at hand.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
2

See this link In particular the section, "Conclusion 4:":

"out" and "ref" are actually exactly the same behind the scenes. The CLR only supports "ref"; "out" is just "ref" where the compiler enforces slightly different rules regarding when the variable in question is known to have been definitely assigned. That's why it is illegal to make method overloads that differ solely in out/ref-ness; the CLR cannot tell them apart! Therefore the rules for type safety for out have to be the same as for ref.

out is implemented in terms of ref, the only differences are in what compiles, and the runtime performance will be identical.

Servy
  • 202,030
  • 26
  • 332
  • 449