Difference between ref and out parameter

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

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.

Introduction

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.

Output

 

 

Sending multiple unassigned variables using out parameter.

 

 

Output

 

 

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.

 

Output

 

 

Sending multiple assigned variables using ref parameter.

 

Output

 

 

Sending unassigned variable using ref paramter gives an error.

 

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

 

 

Conclusion

Happy Reading!

prajapat