c# 指示泛型成员的可空性

Bet*_*ker 5 c# nullable c#-8.0 nullable-reference-types

在这个人为的 C# 8 示例中:

#nullable enable
class Fred<T>
{
    T Value;  // If T is a nullable type, Value can be null.
    public Fred()                 { }
    public void SetValue(T value) { Value = value; }
    public T GetValue()           { return Value; }
    public string Describe()      { return Value.ToString() ?? "oops"; }
}
class George
{
    George()
    {
        Fred<George> fredGeorge = new Fred<George>();
        George g = fredGeorge.GetValue();
        Fred<float> fredFloat = new Fred<float>();
        float f = fredFloat.GetValue();
    }
}
Run Code Online (Sandbox Code Playgroud)

我有三个设计目标:

  1. 如果我为 Fred 编写任何盲目假设“Value”永远不会为空的方法,编译器应该警告我
  2. 如果我在 Fred 之外(比如在 George 中)编写任何盲目地认为“GetValue”永远不会返回 null 的方法,编译器应该警告我。
  3. 没有编译器警告(如果我编写的代码不盲目假设值不会为空)

所以这个第一个版本还不错,我在 Fred 中收到一个警告,Describe() 可能正在取消引用一个空引用(满足目标 #1),但我也收到一个警告,即值在 Fred 的构造函数中未初始化(违反目标 #3)并且 George 编译时没有任何警告(违反目标 #2)。如果我进行此更改:

public Fred() { Value = default; }
Run Code Online (Sandbox Code Playgroud)

George 仍然编译时没有警告(违反目标 #2),我在 Fred 的构造函数中收到一个不同的警告,关于“可能的空引用分配”(违反目标 #3)。

我可以通过使用 null-forgiving 运算符来摆脱可能的 null 引用赋值:

public Fred() { Value = default!; }
Run Code Online (Sandbox Code Playgroud)

现在 Fred 只有正确的警告(可能在 Describe() 中取消引用),但 George 编译时也没有警告(违反目标 #2)。

如果我尝试指出“值”可以为空:

T? Value;
Run Code Online (Sandbox Code Playgroud)

我收到一个编译器错误,提示“必须知道可以为 null 的类型参数是值类型或不可为 null 的引用类型”,所以这不好。

如果我回到

T Value;
Run Code Online (Sandbox Code Playgroud)

并添加“MaybeNull”属性:

[return: MaybeNull]
public T GetValue() { return Value; }
Run Code Online (Sandbox Code Playgroud)

我收到两个警告 - 一个在 Fred.Describe() 警告可能为空取消引用(正确),另一个在 George 警告 fredGeorge.GetValue() 可能为空(正确)。没有关于 fredFloat.GetValue() 为 null(正确)的警告。

因此,在添加代码以期望空引用之后,我最终得到的是:

class Fred<T>
{
    T Value;

    public Fred()
    {
        Value = default!;
    }

    public void SetValue(T value)
    {
        Value = value;
    }

    [return: MaybeNull]
    public T GetValue()
    {
        return Value;
    }

    public string Describe()
    {
        return (Value == null) ? "null" : (Value.ToString() ?? "ToString is null");
    }
}

class George
{
    George()
    {
        Fred<George> fredGeorge = new Fred<George>();
        George? g = fredGeorge.GetValue();
        Fred<float> fredFloat = new Fred<float>();
        float f = fredFloat.GetValue();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是此功能的正确模式吗?

Ili*_*hev 4

其中System.Diagnostics.CodeAnalysis有一个属性AllowNullAttribute指定null允许作为输入,即使相应的类型不允许它。我将在您的最终示例中使用此属性来:

  • 装饰场Value这将允许我们删除null-forgiving operator作业中的Value = default. 编译器不会警告我们Possible null reference assignment,因为现在它知道null可以将值赋给该属性Value
  • 装饰T value该方法的参数SetValue它将允许将null值传递给方法SetValue而不会收到编译器警告Cannot convert null literal to non-nullable reference type(目前,如果我们将null值传递给该方法,SetValue我们将收到此警告)

这是包含建议更改的最终示例:

class Fred<T>
{
    // AllowNull attribute says that a null value
    // can be assigned to the field Value.
    [AllowNull]
    private T Value;

    public Fred()
    {
        // Now we can delete null-forgiving operator, because compiler knows
        // that null value can be assigned to the field Value.
        Value = default;
    }

    // AllowNull attribute says that a null value
    // can be passed to the method SetValue.
    public void SetValue([AllowNull] T value)
    {
        Value = value;
    }

    [return: MaybeNull]
    public T GetValue()
    {
        return Value;
    }

    public string Describe()
    {
        return (Value == null) ? "null" : (Value.ToString() ?? "ToString is null");
    }
}

class George
{
    George()
    {
        Fred<George> fredGeorge = new Fred<George>();
        George? g = fredGeorge.GetValue();

        // Compiler does not warn us "Cannot convert null literal to
        // non-nullable reference type" because it knows that a null
        // value can be passed to the method SetValue.
        fredGeorge.SetValue(null);

        Fred<float> fredFloat = new Fred<float>();
        float f = fredFloat.GetValue();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我们使用常规属性而不是Value具有一对方法的字段GetValueSetValue那么我们可以以更清晰的方式重写最终示例:

class Fred<T>
{
    // Here we tell that:
    // 1) a null value can be assigned;
    // 2) a null value can be returned.
    [AllowNull, MaybeNull]
    public T Value { get; set; }

    public Fred()
    {
        // Compiler does not warn us "Possible null reference assignment".
        // It knows that a null value can be assigned. It is correct.
        // We can delete null-forgiving operator.
        Value = default;
    }

    public string Describe()
    {
        // If we delete null checking, then we get a warning "Dereference of
        // a possibly null reference". It is correct. Compiler helps us to avoid
        // NullReferenceException.
        return (Value == null) ? "null" : (Value.ToString() ?? "ToString is null");
    }
}

class George
{
    George()
    {
        Fred<George> fredGeorge = new Fred<George>();

        // Compiler warns us "Converting null literal or possible null
        // value to non-nullable type". It is correct.
        // We should use nullable reference type George?.
        George g = fredGeorge.Value;

        // Compiler does not warn us "Cannot convert null literal to
        // non-nullable reference type". It knows that a null value
        // can be passed to the method SetValue. It is correct.
        fredGeorge.Value = null;

        Fred<float> fredFloat = new Fred<float>();
        float f = fredFloat.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)