4

I created a regular TryGet method, but added the NotNullWhen(true) attribute to its out parameter, which means that the variable cannot be null if the method returns true. When using the method further, Rider writes a warning that the variable can be null, despite the fact that the method returns true. Why is this happening? Is there a way to get rid of the warning without ignoring it?

private bool TryGet(bool flag, [NotNullWhen(true)]out string? str)
    {
        if (flag)
        {
            str = "568";
            return true;
        }
        else
        {
            str = null;
            return false;
        }
    }

    private int Test()
    {
        var test = TryGet(true, out var str);
        var test2 = test ? int.Parse(str) : 0; //For "str": possible null reference argument
        return test2;
    }
Nikita
  • 41
  • 1

0 Answers0