0

Having the class with the variable and the method like this:

class TheClass
{
    protected string tag_;
    ...
    public string Tag()
    {
        return tag_;
    }
}

the IDE suggests to replace the Tag() method definition like this:

class TheClass
{
    protected string tag_;
    ...
    public string Tag() => tag_;
}

What are the pros and cons? Is the result of compilation and/or optimization of both versions the same?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
pepr
  • 20,112
  • 15
  • 76
  • 139

1 Answers1

1

The second code snippet is a pure syntactic sugar added in C# 6.0. That means it compiles to the exact same IL code as the first snippet and there is absolutely no difference in performance (or otherwise) at runtime. Use whatever style you like.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55