Search code examples
c#parametersrefout

What is the purpose of the "out" keyword at the caller (in C#)?


When a C# function has an output parameter, you make that clear as follows:

private void f(out OutputParameterClass outputParameter);

This states that the parameter does not have to be initialized when the function is called. However, when calling this function, you have to repeat the out keyword:

f(out outputParameter);

I am wondering what this is good for. Why is it necessary to repeat part of the function specification? Does anyone know?


Solution

  • The best answer I got was posted as a comment by plinth:

    The most important reason for the repeating of out/ref is that if the function you're calling gets refactored with a different signature, you will get a compile error. Most notably, if a parameter goes from non-out to out, you'll know right away.