![]() |
VOOZH | about |
The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values.
Important Points:
Declaration of out Parameter:
// No need to initialize // the variable here data_type variable_name; Method_Name(out variable_name); // you can also convert both above two // lines of codes as follows from // C# 7.0 onwards Method_Name(out data_type variable_name);
Here the value of variable_name must be initialized in the called method before it returns a value.
Example:
The addition of the value is: 60
Multiple out Parameters: In C#, a user is allowed to pass multiple out parameters to the method and the method returns multiple values.
Example: In the below code, we declared two value variables without initializing i.e int i, j;. Now we pass these parameters to the Addition method using out keyword like Addition(out i, out j);. The value of these variables is assigned in the method in which they passed.
The addition of the value is: 60 The addition of the value is: 80
Enhancement of Out Parameter in C# 7.0 : In C# 7.0, there are some new features added to the out parameter and the features are:
Example: Below programs demonstrate the inline declaration of Out parameter. Here the line of code i.e Area(out int length, out int width, out int Rarea); contains the inline declaration of Out parameter as these variables are directly declared inside the method calling. The value of the variables is initialized in the method in which they passed.
Note: You need to require C# 7.0 version to run this example.
Example:
Output:
Length of the rectangle is : 30 Width of the rectangle is : 40 Area of the rectangle is : 1200