Skip to main content
 首页 » 编程设计

c#之术语:您如何描述 NullableT> 的类型 T

2024年02月23日14bonelee

在谈论通用类型时,Foo<T> , T被称为泛型类型的泛型参数或类型参数 Foo在 .NET 中 System.Type文档。

现在,当我处理类似 int? 的类型时或 Nullable<Bar> ,在描述类型时如何引用通用参数(即我的示例中的 int 或 Bar)?

我称它为可空类型的基类型,但基类型在描述类层次结构时通常用于指代父类。 .所以我觉得在这里谈论基本类型是不对的。使用可空类型的术语通用参数让我觉得很笼统。

那么你如何谈论这些类型呢?

更具体地说,您将如何命名此扩展方法:

public static System.Type GetXxxType(this System.Type type) 
{ 
    if ((type.IsGenericType) && 
        (type.GetGenericTypeDefinition () == typeof (System.Nullable<>))) 
    { 
        return type.GetGenericArguments ()[0]; 
    } 
    else 
    { 
        return null; 
    } 
} 

当通过一个 Nullable<T>此方法返回 typeof(T)结果。

请您参考如下方法:

来自规范:

A nullable type can represent all values of its underlying type plus an additional null value. A nullable type is written T?, where T is the underlying type

编辑:

引用: C# 4 规范部分 4.1.10 可空类型