【问题标题】:Terminology: How would you describe the type T for Nullable<T>?术语:您如何描述 Nullable<T> 的类型 T?
【发布时间】:2011-09-20 01:43:55
【问题描述】:

谈到泛型类型时,Foo&lt;T&gt;T 在 .NET @ 中称为泛型类型Foo泛型参数类型参数 987654324@ 文档。

现在,当我处理int?Nullable&lt;Bar&gt; 之类的类型时,在描述该类型时如何引用泛型参数(即在我的示例中为intBar)?

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

那么您如何谈论这些类型?

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

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&lt;T&gt; 时,此方法返回typeof(T) 作为结果。

【问题讨论】:

    标签: c# naming-conventions terminology nullable


    【解决方案1】:

    来自规范:

    一个可以为空的类型可以代表所有 其基础类型的值加上一个 额外的空值。可空类型 写成 T?,其中 T 是 底层类型

    编辑:

    参考: C# 4 规范部分 4.1.10 可空类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2015-08-12
      • 1970-01-01
      • 2013-04-19
      • 2017-04-01
      • 2010-10-15
      相关资源
      最近更新 更多