gpt4 book ai didi

.NET 泛型术语 - 打开/关闭、未绑定(bind)/构造

转载 作者:行者123 更新时间:2023-12-03 20:21:46 25 4
gpt4 key购买 nike

.NET 泛型术语有点含糊。更糟糕的是——它似乎在不同的来源中被模棱两可地使用。基本上不清楚的是这四个术语之间的关系(与“类型”有关):

  • 打开
  • 关闭
  • 未绑定(bind)
  • build

  • 我了解 List<T>已打开, List<int>已经关闭。但是,相对于开放/封闭类型,真正的“构造”和“未绑定(bind)”是什么?

    最佳答案

    来自 language specification :

    4.4 Constructed types

    A generic type declaration, by itself,denotes an unbound generic type thatis used as a “blueprint” to form manydifferent types, by way of applyingtype arguments. The type arguments arewritten within angle brackets (< and> )immediately following the name of the generic type. A type that includesat least one type argument is called aconstructed type. A constructed typecan be used in most places in thelanguage in which a type name canappear. An unbound generic type canonly be used within atypeof-expression (§7.6.11).[...]

    4.4.2 Open and closed types

    All types can be classified as eitheropen types or closed types. An opentype is a type that involves typeparameters. More specifically:

    • Atype parameter defines an open type.

    • An array type is an open type if andonly if its element type is an opentype.

    • A constructed type is an opentype if and only if one or more of itstype arguments is an open type. Aconstructed nested type is an opentype if and only if one or more of itstype arguments or the type argumentsof its containing type(s) is an opentype.

    A closed type is a type that isnot an open type.[...]

    4.4.3 Bound and unbound types

    The term unbound type refers to a non-generictype or an unbound generic type. Theterm bound type refers to anon-generic type or a constructedtype. An unbound type refers to theentity declared by a type declaration.An unbound generic type is not itselfa type, and cannot be used as the typeof a variable, argument or returnvalue, or as a base type. The onlyconstruct in which an unbound generictype can be referenced is the typeofexpression (§7.6.11).



    这是我想到的一个例子:
    // Foo<T> is an unbound generic type.
    class Foo<T> { .. }

    // Bar<K> is an unbound generic type.
    // Its base-class Foo<K> is a constructed, open generic type.
    class Bar<K> : Foo<K> { .. }

    // IntFoo is not a generic type.
    // Its base-class Foo<int> is a constructed, closed generic type.
    class IntFoo : Foo<int> { .. }

    这里尝试使用相关属性将其与反射 API 联系起来: IsGenericType , IsGenericTypeDefinitionContainsGenericParameters(根据语言规范,这些测试并不是 100% 预测每种“种类”)。
    +----------+---------------------+-----------+--------------+-------------------+
    | Name | Kind | IsGenType | IsGenTypeDef | ContainsGenParams |
    +----------+---------------------+-----------+--------------+-------------------+
    | Foo<> | Unbound | TRUE | TRUE | TRUE |
    | Foo<>* | Constructed, open | TRUE | FALSE | TRUE |
    | Foo<int> | Constructed, closed | TRUE | FALSE | FALSE |
    | IntFoo | Not generic | FALSE | FALSE | FALSE |
    +----------+---------------------+-----------+--------------+-------------------+
    * = Bar<>'s base type.

    关于.NET 泛型术语 - 打开/关闭、未绑定(bind)/构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4876622/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com