Tom Fischer

  Home  |   Contact  |   Syndication    |   Login
  14 Posts | 0 Stories | 25 Comments | 0 Trackbacks

News

Archives

Post Categories

.NET Framwork

TFS

Tools

Visual Studio

C#: The difference between out and ref.

Today we had an interesting discussion about out and ref in C#.

Here is a very quick elevator pitch summary:

The MSDN documentation (http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx">http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx) is very good on that and it says:
"The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed."

This means inside your method implementation which takes an out parameter you have to set a value before you can use it. At least you have to assign a value before you can return.
This also means you can not use the out paramter to pass a value to the method. (With ref you can)
This also means you can not have any logically includes comparison (e.g. if(param>0 ){ ...}) inside your method before you assign a value to your incoming parameter (I know this sounds weird).

Also nice to know:
"The ref and out keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument."

To see the difference in IL, I used Reflector to see how the IL looks like for out/ref  on Value Types (in my case int32):

out:
[out] int32& result

ref:
int32& result

Remarks:
- Most of the Framework functions (e.g. TryParse) use out and not ref  (which was surprising to me). But inside the internal implementation the Framework mainly uses ref.
- Properties can not be passed via out or ref. (Under the hood they are function calls)
- It is very handy if you have several Value Types as return values and you don't want to create a class or struct.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Wednesday, September 10, 2008 11:29 AM

Feedback

# re: C# and the difference between out and ref 9/10/2008 1:41 PM Randolpho
It might help you to keep in mind the semantic difference between out and ref:

As you mentioned, the ref keyword causes an argument to be passed by reference... by default all arguments are passed by value. This is before the whole value type vs reference type thing comes into play; by default both value and reference types are passed as arguments by value. In the case of a value type the value is copied because the value lives on the stack, while in the case of a reference type only the *reference* -- meaning the pointer into the heap -- lives on the stack, so only the reference is copied.

Sorry about the tangent there. As I was saying:

The purpose of the out keyword is to provide a semantic means of initializing a data type via method call. Functionally it's equivalent to a ref keyword, the only difference is that ref requires that the argument be initialized before it's used, while out requires that the argument *not* be initialized before it's used.

It's there because the c# compiler double-checks that you initialize all types before you use them. Calling a method with a ref keyword counts as such a use, so the compiler insists that you initialize the type first.

This causes problems in the case where you want to have a initialize a type for you. Although many type constructors are relatively cheap, it can in certain edge cases cause performance problems if you force a type to be initialized before it is then initialized in another method -- it's essentially a waste of resources.

But the designers didn't want to give up the safety that requiring initialization grants programmers, so they came up with the out keyword. By adding a second functionally equivalent but semantically opposite keyword, the designers maintained the initialization requirement while eliminating the resource waste for initialization methods.

It's a pretty elegant solution, when you think about it.



# re: C# and the difference between out and ref 9/12/2008 8:30 AM rusty
Hmm. I guess I just had not thought so much about the differences. I don't generally use ref and only recently have started using {type}.TryParse.

Good post, it made me think more which is good even on a Friday.

rusty

# re: C# and the difference between out and ref 11/12/2008 6:30 AM Rupesh Sukhalal Shingavi
It is really a nice explination of difference bet ref and out parameters.
Thanks a lot.

# re: C# and the difference between out and ref 3/24/2010 10:09 AM Phong
Randolpho is wrong in so many ways it's not even funny. Firstly, out absolutely does not require you to pass in an uninitialized variable.
As a result, out and ref are not at all "semantically opposite".
Finally, you can initialize a reference type by assigning it to null. There is no need to call any potentially expensive constructor.

# re: C# and the difference between out and ref 12/14/2010 6:48 AM Shallwe Laugh
Here is a nice article on this

http://dotnetrobert.com/?q=node/209


# re: C# and the difference between out and ref 5/8/2011 3:37 AM Saurabh Mahajan
Very nice article.....
Thanks


# re: C# and the difference between out and ref 9/18/2011 4:56 AM Mamta
really nice post :)

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: