1

Consider the following C#.Net code:

using System.Xml;

internal class MyXmlReaderClass : XmlReader
{
    private readonly XmlReader _innerReader;

    public MyXmlReaderClass(...other_parameters... , XmlReader innerReader)
    {
        _innerReader = innerReader;
    }
    //... lots of code has been left out, here ...

    public override string LocalName
    {
        get { return _innerReader.LocalName; }
    }
    //... lots more code has been left out, here ... 
}

In the property, LocalName, ReSharper places an underline under the type, string, with the warning, 'string' is (explicitly or implicitly) [NotNull].

I thought that I understood the concept in play regarding nullable objects. I have been able to refactor most of these issues out of my code, except in this case.

If I understand my world correctly, the base class (XmlReader) is what ReSharper identifies as [NotNull]. That means that ReSharper believes that my override of the property could possibly return a null while the base never returns a null.

I am having a difficult time wrapping my mind around how I could override the base class property LocalName with some sort of override, such that there is no conflict in the property between the base class and the derived class.

My question is a multiple-part, "What exactly is ReSharper identifying, and how do I fix the inconsistency?"

Yes, I have searched high and low through StackOverflow and used Google yet cannot find this specific example.

Thank you.

ulrichb
  • 19,610
  • 8
  • 73
  • 87
NW7US
  • 93
  • 3
  • 10

1 Answers1

3

This is not a ReSharper warning, it's a "type highlighting" of the Implicit Nullability extension.

What exactly is ReSharper identifying?

The highlighting informs you that your member's return type is implicitly [NotNull] because the base member is [NotNull].

The base member, XmlReader.LocalName, is [NotNull] because of an External Annotation provided by ReSharper.

How do I fix the inconsistency?

You don't have to fix it, because it's no warning/suggestion. It's just a highlighting.

If you don't like this highlighting you can disable it in the R# options on the Implicit Nullability page.

ulrichb
  • 19,610
  • 8
  • 73
  • 87