Are local functions in properties a good idea or a sign the property should be it's own method?

587 views Asked by At

If I did something like this:

public int Foo
{
    get
    {
        return GetFoo() + 5;

        int GetFoo() => 3;
    }
}

It will actually compile, which makes me wonder if this would be a good or bad practice.

Now, clearly this particular example can be refactored into: public int Foo => 8;, but other cases might have a few local methods and doing stuff in it's main get portion.

Keeping in mind the fact that C# actually moves GetFoo outside of property or method it is in whenever the code is compiled, this should have just about the exact same performance as a property calling a normal method.

However, If one feels the need to use local functions in a property, is that a sign that the property is doing too much work and should be a method instead?

0

There are 0 answers