Search code examples
c#recursionresharpernameof

Is this nameof() truly a recursive call?


I'm making a ConfigurationSection in C# right now. I decided to take advantage of some C# 6 features by using nameof(PropertyName) instead of hard coded strings.

However, I'm getting a weird result from Resharper about a recursive call.

Here's an example:

Recursive Warning

I can see why it might give this warning, as the property is being referenced within itself. I'm not convinced however that this is actually recursive.

Is there anything I have to worry about with this call?


Solution

  • Unless this[string] calls CoreApiRootUrl, then it's not recursive.

    You're right that nameof(...) doesn't actually make a call at all.

    I've just validated that I see the same bug (with R# 10.0.1) in a small test class:

    public class Bug
    {
        public string this[string x] => "";
    
        public string Foo
        {
            get { return this[nameof(Foo)]; } // Warning here
        }
    }
    

    Oddly enough, if I change Foo to an expression-bodied property:

    public string Foo => this[nameof(Foo)];
    

    I don't see the warning.

    Definitely a R# bug - file it with Jetbrains.