Skip to main content
Tweeted twitter.com/StackSoftEng/status/1163511150518177797
edited title
Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

C# Declaring return variables in c# methods vs returning the value directly

Source Link
pb01
  • 313
  • 2
  • 5

C# return variables

In a debate regarding return variables, some members of the team prefer a method to return the result directly to the caller, whereas others prefer to declare a return variable that is then returned to the caller (see code examples below)

The argument for the latter is that it allows a developer that is debugging the code to find the return value of the method before it returns to the caller thereby making the code easier to understand: This is especially true where method calls are daisy-chained.

Are there any guidelines as to which is the most efficient and/or are there any other reasons why we should adopt one style over another?

Thanks

    private bool Is2(int a)
    {
        return a == 2;
    }

    private bool Is3(int a)
    {
        var result = a == 3;
        return result;
    }