How to use the in, out and ref keywords in .NET Core
The in
, out
And ref
Keywords are widely used keywords in C#. They allow us to create better abstractions for data types and methods, making our code more readable and maintainable.
Both in
Keywords and out
The keyword allows you to pass parameters to a method by reference. The out
The keyword allows you to change the value of the parameter, where in
Keywords don’t let you change them.
you can use ref
keyword to pass input and output parameters by reference, allowing the call method to modify those arguments as needed. By passing parameters by reference, ref
The keyword ensures that changes to parameters within the method are reflected outside the method.
In this article, we’ll explore each of these keywords in more detail so you know how and when to use them when working with C#. To work with the code examples provided in this article, you should have Visual Studio 2022 Preview installed on your system. If you don’t already have a copy, you can download Visual Studio 2022 Preview here.
Create a console application project in Visual Studio
First, let’s create a .NET Core console application project in Visual Studio. Assuming you have Visual Studio 2022 Preview installed on your system, follow the steps outlined below to create a new .NET Core Console Application project in Visual Studio.
- Launch the Visual Studio 2022 Preview IDE.
- Click on “Create New Project”.
- In the “Create New Project” window, select “Console App (.NET Core)” from the list of templates displayed.
- Click Next.
- In the “Configure your new project” window that appears next, specify the name and location of the new project.
- Click Next
- In the “Additional Information” window that appears next, specify .NET 7 as the .NET version you want to use.
- Click Create.
This will create a new .NET Core 7 Console Application project in Visual Studio 2022 Preview. We will use this project to work with it in
, out
And ref
Keywords in the next section of this article.
The ref keyword in C#
In C#, this is accomplished using passing an object or variable by reference ref
Keywords Normally, when a variable is passed to a method, the value of the variable is copied into the method so that it can be used further.
But if we use ref
To pass a variable by reference, the variable is not copied into the method. Instead, the method follows the reference to access the parent variable. So any change to the value of the variable made in the called method will be made to the original variable.
It should be noted that you must specify ref
keyword in both the method signature and the point at which the method is called. Consider the following code:
void RefDemo(ref int refParameter)
{
refParameter = refParameter + 1;
}
int number = 1;
RefDemo(ref number);
Console.WriteLine(number);
When you run the preceding code, the number 2 will appear in the console. Because this variable is called number
Passed by reference and value refParameter
Inside is incremented by 1 RefDemo
method
The following code snippet demonstrates how you can use ref
Keywords with objects.
void RefDemo(ref int refParameter)
{
refParameter = refParameter + 1;
}
int number = 1;
RefDemo(ref number);
Console.WriteLine(number);
When you execute the preceding code, the text “Hello World” will be displayed on the console.
An interesting point here. If you run the following piece of code, then RefDemo
The method will return true because of its reference str
And refParameter
The object is the same.
string str = "Hello";
string str = "Hello";
bool RefDemo(ref string refParameter)
{
refParameter = "Hello World";
return ReferenceEquals(str, refParameter);
}
bool isEqual = RefDemo(ref str);
Console.WriteLine(isEqual? "The two references are equal":"The two references are not equal");
The in keyword in C#
The in
The keyword is used in C# to specify that a method parameter is passed by reference, but cannot change the called method argument. This is useful for parameters that are not changed by the calling method, but must be passed by reference to the calling method to access the results.
Figure 1 shows that you cannot change the value of an in parameter.
Figure 1. No! The in
Does not allow changing keyword parameter values.
The out keyword in C#
The out
The keyword ref works the same way as the keyword; This allows you to pass parameters by reference and change the value of that parameter. The out
The keyword is identical to ref
Keyword, except that ref
Variables must be initialized before being passed. while working with out
In C# both keywords, method signatures and calling methods must be explicitly specified out
keyword
For example, a method must return both a success code and a value. Using an output parameter for the variable (say, retValue
), the method can directly return the success code and use the output parameter for the value.
The following code snippet illustrates how you can work with this out
Keywords in C#.
int number;
OutDemo(out number);
Console.WriteLine(number); //The value is now 100
void OutDemo(out int number)
{
number = 100;
}
When you run the preceding code, the value 100 will be displayed in the console.
In, out, and ref limitations
You cannot use in
, out
And ref
keyword in async methods or iterative methods, such as those that use a yield return
or yield break
In addition to the statement, you cannot use in
keyword in the first parameter to an extension method, unless the parameter is a struct
.
Note keywords that are relevant in
, ref
And out
Do not form part of the method signature when defining an overload. Therefore, you cannot overload methods that only distinguish between signatures for these keywords.
In other words, if you have two methods with the same name, one of them accepts an integer in
parameter and takes the other as an integer out
parameter, your code will not compile at all.
Copyright © 2022 IDG Communications, Inc.