Explanation in a synthetic way about the functioning of the keywords ref and out, exclusive to the C # language.
Before going further in this article, it is necessary to understand the difference between value types and reference types.
ref
To fully understand ref, we will directly take a piece of code that uses this keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; public class Program { public static void Main() { int a = 10; Add10(ref a); Console.WriteLine(a); // Output : 20 } public static void Add10(ref int a) { a += 10; } } |
As you can see, the keyword must be used as an argument as well when declaring a method as during its call. ref will therefore allow us to pass the reference of the variable in order to directly modify its value without going through the creation of a copy. Indeed, when passing a value argument, it is a copy that we manipulate and not directly its value, unlike reference types. So our piece of code will return 20. The absence of the keyword would have returned to us 10.
out
The out keyword allows a method to return multiple results in addition to its return value. Indeed, thanks to the use of this key word, you will be able to declare variables without initializing them and affect them when they pass in the body of a method through its parameters. Be careful, the method containing a parameter out must necessarily affect the associated variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; public class Program { public static void Main() { int a; Set10(out a); Console.WriteLine(a); // En sortie : 10 } public static void Set10(out int a) { a = 10; } } |
So, in this very simple example, we observe a value type (a) that does not need to be initialized by using the out keyword.
A concrete example of using this keyword is the TryParse method of the Int32 class. This method allows you to return two things:
- To check if it is possible to convert a string to an int (the return parameter of the function returns this possibility or not by a Boolean type)
- To be able, in the possible case of a conversion, to assign this string in an int passed in parameter with the keyword out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp14 { class Program { static void Main(string[] args) { int number; bool result = int.TryParse("1234", out number); Console.WriteLine("number : {0} , conversion to int possible ? {1}", number, result); int notANumber; result = int.TryParse("bonsoir", out notANumber); Console.WriteLine("notANumber : {0}, bonsoir, conversion to int possible ? {1}.", notANumber, result); //Output : //number : 1234 , conversion to int possible ? True //notANumber : 0, bonsoir, conversion to int possible ? False. Console.ReadKey(); } } } |
Note that for the second test, notANumber takes the default value of an int if the conversion is impossible: ie zero.
Я уверен, что статья затронет всех пользователей
авторского и действительно очень научного блога.