sách gpt4 ăn đã đi

C# Language : generics, open/closed, bound/unbound, constructed

In lại 作者:IT王子 更新时间:2023-10-29 04:02:44 27 4
mua khóa gpt4 giày nike

我正在阅读 Anders Hejlsberg 等人撰写的《C# 编程语言》第 4 版。

有几个定义有点曲折:

未绑定(bind)的泛型类型:泛型类型声明本身表示未绑定(bind)的泛型类型......

构造类型:至少包含一个类型参数的类型称为构造类型。

开放类型:开放类型是涉及类型参数的类型。

封闭类型:封闭类型是一种非开放类型。

未绑定(bind)类型:指非泛型类型或未绑定(bind)泛型。

绑定(bind)类型:指非泛型类型或构造类型。[注释] ERIC LIPPERT:是的,非泛型类型被认为是绑定(bind)和未绑定(bind)的。

问题 1,下面我列出的是正确的吗?

int //non-generic, closed, unbound & bound, 
class A //generic, open, unbound,
class A //generic, open, bound, constructed
class A //generic, open, bound, constructed
class A //generic, closed, bound, constructed

问题 2,书上说“未绑定(bind)类型是指由类型声明声明的实体。未绑定(bind)泛型类型本身不是类型,它不能用作变量、参数或返回值的类型,也不能用作基类型。唯一可以在其中引用未绑定(bind)泛型类型的构造是 typeof 表达式(第 7.6.11 节)。”很好,但下面是一个可以编译的小测试程序:

public class A { }

// Q2.1: how come unbounded generic type A can be used as a base type?
public class B : A { }

public class C
{
// Q2.2: how come unbounded generic type Dictionary can be used as a return value?
public Dictionary ReturnDictionary() { return new Dictionary(); }

// Q2.3: how come unbounded generic type A can be used as a return value?
public A ReturnB() { return new A(); }
}

câu trả lời hay nhất

这些是未绑定(bind)泛型类型Ví dụ:

  • List<>
  • Dictionary<,>

它们可以与 loại của 一起使用,即以下是有效的表达式:

  • typeof(List<>)
  • typeof(Dictionary<,>)

这应该可以回答您的问题 2。关于问题 1,请注意类型参数 可以是构造类型 hoặc类型参数。因此,您的列表应更新如下:

public class MyClass { // declares the type parameters T and U

// all of these are
// - generic,
// - constructed (since two type arguments are supplied), and
// - bound (since they are constructed):

private Dictionary var1; // open (since T and U are type parameters)
private Dictionary var2; // open (since T is a type parameter)
private Dictionary var3; // closed
}

关于C# Language : generics, open/closed, bound/unbound, constructed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6607033/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress