C# Keywords Tutorial Part 61: out

C# Keywords Tutorial Part 61: out

C# is a strong and adaptable programming language that enables programmers to design intricate and reliable programs. The “out” keyword is only one of the numerous elements that contribute to C#’s flexibility. In order to assist you better grasp how to utilize the “out” keyword, we’ll go through what it is in this blog article along with some samples of code.

To provide a variable to a method as a reference argument, use the “out” keyword. This indicates that the method has the ability to change the variable’s value, and that such changes will remain in effect even after the method has finished. Variables are frequently supplied as value parameters without the “out” keyword, which implies that any changes made to the variable while the function is running are lost once the method has finished.

Here’s an example to illustrate this point:

static void Main(string[] args)
{
    int x = 5;
    Increment(x);
    Console.WriteLine(x); // Output: 5
}

static void Increment(int value)
{
    value++;
}

In this illustration, the variable x has a default value of 5. Then, we call the Increment method and supply the value of x. We add one to the value parameter within the Increment method. The function is called, but when we attempt to print the value of x thereafter, we still receive the initial value of 5. This is so because any changes made to the value parameter, which is a duplicate of the original variable, are not preserved.

Now, let’s look at how we can use the “out” keyword to pass a variable as a reference parameter:

static void Main(string[] args)
{
    int x = 5;
    Increment(ref x);
    Console.WriteLine(x); // Output: 6
}

static void Increment(ref int value)
{
    value++;
}

The “ref” keyword has been added before the value parameter in the Increment method in this modified example. This suggests that the variable should be given as a reference argument. Additionally, we updated the procedure to use the “out” keyword rather than “ref,” which in this situation has the same result. Because the value of x was changed by the Increment function, when we run the code right now, we obtain the anticipated result of 6.

The “out” keyword is particularly useful when a method needs to return multiple values. Instead of creating a custom object to hold these values, you can simply use the “out” keyword to pass variables as reference parameters and modify them within the method. Here’s an example:

static void Main(string[] args)
{
    int x;
    int y;
    GetCoordinates(out x, out y);
    Console.WriteLine($"x: {x}, y: {y}"); // Output: x: 10, y: 20
}

static void GetCoordinates(out int x, out int y)
{
    x = 10;
    y = 20;
}

In this example, we have a method called GetCoordinates that returns the x and y coordinates of a point. We use the “out” keyword to pass x and y as reference parameters and modify their values within the method. When we call the method and print out the values of x and y, we get the expected output of x: 10, y: 20.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *