Here we will learn what is ref keyword in c#, uses of ref keyword, what is out keyword in c#, uses of out keyword, and the differences between ref and out keywords in c# with examples.
In c#, while calling any function with parameters, we will send the copy of the variable to the function instead of its original value, which means the changes we do inside the function will not affect the outside variable value. To understand this, we will create a simple method example in c#.
In this function example, we are updating the value of variables inside the method. The updates that we do inside the function will not affect the actual value of variables. When we execute the above example, we will get the result as shown below.
If you observe the result, the changes that we did inside the function didn’t affect the outside of the variable values as we only send a copy of the variables.
If you want to change the value of variables when there is a change in our methods that can be achieved by using ref
and out
keywords.
In c#, the ref keyword is useful to send the reference of a variable to the method and the changes that we do inside the method will also affect the outside variable.
Basically, the functions/methods in c# will return only one value that is either an integer, string, list, etc. If you want to return more than one value, the ref
keyword will be useful for us.
Following is the syntax of defining the variables and passing parameters to the method using the ref
keyword in c#.
To pass parameters using ref
keyword, you need to explicitly define the parameters with the ref
keyword in both the method definition and while calling the method.
Before sending parameters using the ref
keyword, you need to remember that the variables that are passing as method arguments using the ref
keyword must be initialized before sending it to the method otherwise, we will get the “Use of unassigned local variable” compilation error as shown below.
Following is an example of using the ref
keyword to send parameters to the method as reference type and return the multiple values from the method in c#.
In this ref parameter example, we explicitly specified the variables using the ref keyword in both the method definition (ModifyValues) and while calling the method.
When you execute this example, you will get the result as shown below.
If you observe the result, the changes that we did inside the method have affected the variables that are outside of the function, and we are able to return the multiple values from the ModifyValues method.
To learn more about the ref parameter in c#, visit Ref Parameter in C# with Examples.
In c#, the out
keyword is same as the ref
keyword to pass method arguments as reference types, but the only difference is you don’t need to initialize the variable that you want to pass as a method argument but the method must assign a value to the variable before it returns the value.
Following is the syntax of defining the variables and passing parameters to the methods using out
parameter in c#.
Same as ref
keyword, to pass parameters using out
keyword you need to explicitly define the parameters with out
keyword in both the method definition and while calling the method.
Before passing parameters using the out
keyword, you need to remember that the variables that are passing as method arguments using the out
keyword must be initialized before sending it to the method otherwise, we will get the “Use of unassigned local variable” compilation error as shown below.
Following is an example of using out
parameter to send parameters to the method as reference type and return the multiple values from the method in c#.
In this out
parameter example, we explicitly specified the variables using the out
keyword in both the method definition (ModifyValues) and while calling the method.
When you execute this example, you will get the result as shown below.
To learn more about out parameter in c#, visit Out Parameter in C# with Examples.
As discussed, the c# ref
and out
keywords are useful to pass method arguments as reference types and return multiple values from a function.
The following table lists the differences between ref and out in c#.
Ref | Out |
---|---|
The variables that are passing as arguments using the ref keyword must be initialized. | It's not mandatory to initialize the variables to pass as arguments using out keyword. |
It's not mandatory to initialize the variable before returning the value to the calling method. | It's mandatory to initialize the variable in the method before returning the value to the calling method. |
Useful to return multiple values from methods. | Useful to return multiple values from methods. |
To learn more about the ref keyword, out parameter, and other c# topics, visit our C# Tutorial.