可空的引用类型:如何指定“ T”?类型而不限制类或结构

And*_*ent 19 c# nullable c#-8.0 nullable-reference-types

我想创建一个具有type成员的泛型类TT可以是类,可为空的类,结构或可为空的结构。所以基本上什么都可以。这是一个简化的示例,显示了我的问题:

#nullable enable

class Box<T> {
    public T Value { get; }

    public Box(T value) {
        Value = value;
    }

    public static Box<T> CreateDefault()
        => new Box<T>(default(T));
}
Run Code Online (Sandbox Code Playgroud)

由于使用了新#nullable enable功能,我得到以下警告:Program.cs(11,23): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type.

这个警告对我来说很有意义。然后,我尝试通过?在属性和构造函数参数中添加来修复此问题:

#nullable enable

class Box<T> {
    public T? Value { get; }

    public Box(T? value) {
        Value = value;
    }

    public static Box<T> CreateDefault()
        => new Box<T>(default(T));
}
Run Code Online (Sandbox Code Playgroud)

但是现在我得到两个错误:

Program.cs(4,12): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
Program.cs(6,16): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
Run Code Online (Sandbox Code Playgroud)

但是,我不想添加约束。我不在乎T是类还是结构。

一个明显的解决方案是将有问题的成员包装在#nullable disable指令下。但是,就像一样#pragma warning disable,除非必要,否则我想避免这样做。是否有另一种方法可以在不禁用可空性检查或CS8653警告的情况下编译我的代码?

$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview4-011223
 Commit:    118dd862c8
Run Code Online (Sandbox Code Playgroud)

Rik*_*son 23

如果您使用的是 C# 9,该怎么办

在 C# 9 中,您可以T?在不受约束的类型参数上使用,以指示当 T 是引用类型时该类型始终可为空。实际上,在添加?到属性和构造函数参数后,原始问题中的示例“仅起作用” 。请参阅以下示例以了解您对 的不同类型的类型参数可能期望的行为Box<T>

var box1 = Box<string>.CreateDefault();
// warning: box1.Value may be null
box1.Value.ToString();

var box2 = Box<string?>.CreateDefault();
// warning: box2.Value may be null
box2.Value.ToString();

var box3 = Box<int>.CreateDefault();
// no warning
box3.Value.ToString();

var box4 = Box<int?>.CreateDefault();
// warning: 'box4.Value' may be null
box4.Value.Value.ToString();
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 C# 8,该怎么办

在 C# 8 中,不可能在不受约束的类型参数(即,不知道是引用类型还是值类型)上放置​​可为空的注释。

正如对此问题的评论中所讨论的那样,您可能需要考虑Box<string>具有默认值的 a 在可空上下文中是否有效,并可能相应地调整您的 API 表面。也许类型必须是Box<string?>为了使包含默认值的实例有效。但是,在某些情况下,您需要指定属性、方法返回或参数等仍然可以为 null,即使它们具有不可为 null 的引用类型。如果您属于该类别,您可能希望使用与可空性相关的属性。

MAYBENULL允许为空的属性已被引入到.NET核心3处理这种情况。

这些属性的一些具体行为仍在进化中,但基本思想是:

  • [MaybeNull]意味着某些东西的输出(读取字段或属性、方法返回等)可能是null.
  • [AllowNull]意味着对某事的输入(编写字段或属性、方法参数等)可能是null.
#nullable enable
using System.Diagnostics.CodeAnalysis;

class Box<T>
{
    // We use MaybeNull to indicate null could be returned from the property,
    // and AllowNull to indicate that null is allowed to be assigned to the property.
    [MaybeNull, AllowNull]
    public T Value { get; }

    // We use only AllowNull here, because the parameter only represents
    // an input, unlike the property which has both input and output
    public Box([AllowNull] T value)
    {
        Value = value;
    }

    public static Box<T> CreateDefault()
    {
        return new Box<T>(default);
    }

    public static void UseStringDefault()
    {
        var box = Box<string>.CreateDefault();
        // Since 'box.Value' is a reference type here, [MaybeNull]
        // makes us warn on dereference of it.
        _ = box.Value.Length;
    }

    public static void UseIntDefault()
    {
        // Since 'box.Value' is a value type here, we don't warn on
        // dereference even though the original property has [MaybeNull]
        var box = Box<int>.CreateDefault();
        _ = box.Value.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅https://devblogs.microsoft.com/dotnet/try-out-nullable-reference-types了解更多信息,特别是“ T 的问题? ”部分。

  • 当 @JonSkeet 称其为“设计中最棘手的部分”时,您就知道这确实是一个棘手的问题!就我个人而言,我认为我们在语言方面走上了错误的道路,我相信不可为空的引用类型应该是完整类型。事实上,在 C#9 中,T 不受约束(甚至约束为 notnull),语法“T?” 表示“对应于 T 的默认类型”,而不是“对应于 T 的可为空类型”,就像语言中其他地方一样。但是,如果您将 T 限制为类或结构,那么“T?” 再次开始表示“与 T 对应的可为空类型”。 (2认同)

And*_*ent 4

Jeff Mercado 在评论中提出了一个很好的观点:

我认为你在这里有一些相互矛盾的目标。您想要默认框的概念,但对于引用类型,还有什么是合适的默认值?引用类型的默认值为 null,这与使用可为 null 的引用类型直接冲突。也许您需要将 T 限制为可以默认构造的类型(new())。

例如,default(T)forT = string将是,因为在运行时,和null之间没有区别。这是该语言功能的当前限制。stringstring?

我通过CreateDefault为每种情况创建单独的方法来解决这个问题:

#nullable enable

class Box<T> {
    public T Value { get; }

    public Box(T value) {
        Value = value;
    }
}

static class CreateDefaultBox
{
    public static Box<T> ValueTypeNotNull<T>() where T : struct
        => new Box<T>(default);

    public static Box<T?> ValueTypeNullable<T>() where T : struct
        => new Box<T?>(null);

    public static Box<T> ReferenceTypeNotNull<T>() where T : class, new()
        => new Box<T>(new T());

    public static Box<T?> ReferenceTypeNullable<T>() where T : class
        => new Box<T?>(null);
}
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎是类型安全的,但代价是更丑陋的调用站点(CreateDefaultBox.ReferenceTypeNullable<object>()而不是Box<object?>.CreateDefault())。在我发布的示例类中,我只是完全删除这些方法并Box直接使用构造函数。那好吧。