Geeks With Blogs
Tom Fischer
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.

 

Posted on Wednesday, September 10, 2008 11:29 AM C# , .NET Framework | Back to top


Comments on this post: C# and the difference between out and ref

# re: C# and the difference between out and ref
Requesting Gravatar...
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.

Left by Randolpho on Sep 10, 2008 1:41 PM

# re: C# and the difference between out and ref
Requesting Gravatar...
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
Left by rusty on Sep 12, 2008 8:30 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
It is really a nice explination of difference bet ref and out parameters.
Thanks a lot.
Left by Rupesh Sukhalal Shingavi on Nov 12, 2008 6:30 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
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.
Left by Phong on Mar 24, 2010 10:09 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
Here is a nice article on this

http://dotnetrobert.com/?q=node/209
Left by Shallwe Laugh on Dec 14, 2010 6:48 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
Very nice article.....
Thanks
Left by Saurabh Mahajan on May 08, 2011 3:37 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
really nice post :)
Left by Mamta on Sep 18, 2011 4:56 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
really simple and good example
Left by vasanth on Jun 29, 2012 7:19 AM

# re: C# and the difference between out and ref
Requesting Gravatar...
really good post
Left by Deepak Gupta on Jul 18, 2012 12:11 PM

Your comment:
 (will show your gravatar)
 


Copyright © ftom | Powered by: GeeksWithBlogs.net | Join free