Search code examples
c#methodsout

How to pass a variable into a function and return it modified?


I've been learning c# for about three weeks now so please bear with me. I got through some online courses with no problem but I've come upon what I think is a very basic, but very real barrier to my understanding of how to use the code to actually write programs.

int inputOutput = 1;
Console.WriteLine(inputOutput); //Prints 1
static void IncrementTest(int inputInput, out int intputOutput)
{
    intputOutput = inputInput++;
}
IncrementTest(inputOutput, out inputOutput);
Console.WriteLine(inputOutput); //Also prints 1?

If using out parameters is not the way forward, what is?


Solution

  • The out keyword is for parameters that are used for output only. If you want to use a parameter for input and output then you use ref. When a parameter is unadorned, it is considered input-only, e.g.

    public static void Main()
    {
        var number = 1;
        Console.WriteLine("Outside Method before: " + number);
        Method(number);
        Console.WriteLine("Outside Method after: " + number);
    }
    
    public static void Method(int inputOnly)
    {
        Console.WriteLine("Inside Method before: " + inputOnly);
        inputOnly++;
        Console.WriteLine("Inside Method after: " + inputOnly);
    }
    

    Output:

    Outside Method before: 1
    Inside Method before: 1
    Inside Method after: 2
    Outside Method after: 1
    

    When a parameter is declared out, it is considered output-only. This code:

    public static void Main()
    {
        var number = 1;
        Console.WriteLine("Outside Method before: " + number);
        Method(out number);
        Console.WriteLine("Outside Method after: " + number);
    }
    
    public static void Method(out int outputOnly)
    {
        Console.WriteLine("Inside Method before: " + outputOnly);
        outputOnly++;
        Console.WriteLine("Inside Method after: " + outputOnly);
    }
    

    will not compile because any existing value for an out parameter is ignored and it is considered uninitialised at the beginning of the method. You MUST assign a value to an out parameter before using it and, if you don't use it, before the end of the method.

    public static void Main()
    {
        var number = 1;
        Console.WriteLine("Outside Method before: " + number);
        Method(out number);
        Console.WriteLine("Outside Method after: " + number);
    }
    
    public static void Method(out int outputOnly)
    {
        outputOnly = 10;
        Console.WriteLine("Inside Method before: " + outputOnly);
        outputOnly++;
        Console.WriteLine("Inside Method after: " + outputOnly);
    }
    

    Output:

    Outside Method before: 1
    Inside Method before: 10
    Inside Method after: 11
    Outside Method after: 11
    

    When a parameter is declared ref, it is considered input-output, e.g.

    public static void Main()
    {
        var number = 1;
        Console.WriteLine("Outside Method before: " + number);
        Method(ref number);
        Console.WriteLine("Outside Method after: " + number);
    }
    
    public static void Method(ref int inputOutput)
    {
        Console.WriteLine("Inside Method before: " + inputOutput);
        inputOutput++;
        Console.WriteLine("Inside Method after: " + inputOutput);
    }
    

    Output:

    Outside Method before: 1
    Inside Method before: 1
    Inside Method after: 2
    Outside Method after: 2