Difference between ref and out parameter

Posted by in C# category on for Beginner level | Points: 250 | Views : 14856 red flag
Rating: 5 out of 5  
 2 vote(s)

Recently, I have come across a query in a forum. The query was something like 'difference between ref and out parameters'. Hence, I have just tried to point out the difference between the two.


We can pass ref and out parameters through the methods as arguments.

Out Parameter

In case of out parameter , the caller method need not assign a value to the out parameter. Instead the callee method is responsible to assign value in the out parameter.

Let me show u examples:

Sending single unassigned variable using out parameter.




Sending multiple unassigned variables using out parameter.




Ref Parameter

Whereas, in case of ref parameter, the caller method must assign a value to the ref parameter. Here the callee method may change the value of the ref parameter.  

Sending single assigned variable using ref parameter.



Sending multiple assigned variables using ref parameter.



Sending unassigned variable using ref paramter gives an error.

Error Message on trying to send unassigned variable using ref parameter.



Happy Reading!


http:// www. dotnetspider.com/forum/156002-What-difference-between-out-ref-C.aspx
http:// www. c-sharpcorner. com/UploadFile/mahesh/out_and_ref11112005002102AM/out_and_ref.aspx
http:// dotnetperls.com/ref
http:// dotnetrobert .com/?q=node/209

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Zinnia Sarkar
Member Level:
Member Status: Member
Member Since: 7/24/2010 12:50:40 PM
Country: India
Regards, Ganeshji

Login to vote for this post.

Comments or Responses

Posted by: Ck.kislay on: 1/21/2011 | Points: 25
Only Out parameter is sufficient to achieve the goal.

Let me know any specific reason so that .net framework provde the ref parameter??

Posted by: Ganeshji on: 1/21/2011 | Points: 25
A method parameter can be modified by ref.
Zinnia Sarkar
Posted by: Vijayakumarp on: 3/19/2012 | Points: 25
Thanks for great article..

Only i found the difference between these two parameters(out,ref) that the ref parameter variable should be assigned value..

Posted by: Ganeshji on: 3/19/2012 | Points: 25
Thanks Vijayakumarp!

Login to post response

Comment using Facebook(Author doesn't get notification)