.Net Interview Questions and Answers

Search:

What is the difference between Out and Ref? (C# Interview questions)

By : Shiv Prasad Koirala | Sep 29th, 2015 | Views : 3655 | Comments : 0 Bookmark and Share

Out and Ref are C# keywords which helps to pass variables in methods and functions as REFERENCE types. Out is one way and Ref is two way.


Let us try to understand the above sentence. Below is a simple function "SomeFunction" which takes a variable "InsideVar".

Now assume there is a caller who calls the above method as shown in the below code. After the call is made to "SomeFunction" , "OutSideVar" variable is not modified. In other words by default values are passed to methods and function BY VAL.



Now let us apply REF keyword to the method variable as shown in the below code.



If you decorate method variables using REF you also need to provide REF keyword in the caller as shown in the below code. Now in this situation the output of "OutSideVar"  will be "30". In other words the variable is passed by reference. Any modification to the REF variables inside the methods and function will be affected to outside variables as well.



OUT is same like REF keyword ,  it also passes variables by reference but the OUT variables compulsorily needs to be initialized inside the methods and functions.


Which means when the caller passes data to a method which is having OUT variables those data will be discarded. In the below case "20" value is passed from the caller but this data will be discarded as the OUT variables will be initialized again inside the method / function.



So lets summarize so that interviewer does not take us for a ride:-


  • OUT and REF helps to pass variables BY REFERENCE.

  • In OUT we need to compulsorily initialize variables inside the function for REF we do not need to do the same.

  • REF helps to pass data two ways i.e. from caller to callee and back. While OUT is one way it just passes data from callee to caller.


Below is a nice C# video which demonstrates the difference between OUT and REF keyword of C#




While writing this interview question I referred this article from codeproject


C# Out VS Ref

 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
Article Tags : C , Vs Ref , NET , ASPNET ,
 
Comments

Write a Comment

All fields marked with * are mandatory

a7e2b

 

 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi