[英]What does the '1 mean when I GetType().Name on a generic type?
[英]What does “A type can name itself as the concrete type when closing a type argument” mean?
从C#总结
关闭类型参数时,类型可以将其自身命名为具体类型
public interface IEquatable<T> { bool Equals (T obj); } public class Balloon : IEquatable<Balloon> { public string Color { get; set; } public int CC { get; set; } public bool Equals (Balloon b) { if (b == null) return false; return b.Color == Color && b.CC == CC; } }
以下也是合法的:
class Foo<T> where T : IComparable<T> { ... } class Bar<T> where T : Bar<T> { ... }
“关闭类型参数时,类型可以将自己命名为具体类型”是什么意思? 您能否显示示例中的位置?
“具体类型”和“关闭类型参数”是什么意思?
在示例中,没有泛型类型的实例化,那么“类型实参”和“具体类型”在哪里?
谢谢。
Balloon
类是混凝土类型。 您可以使用它来创建Balloon
类型对象的实例,如下所示:
var balloon = new Balloon
{
CC = 1;
Color = "Blue"
};
另一方面, IEquatable<T>
是通用接口。 只要有一个类型实现此接口,就用该类型替换T
因此,如果要让名为Balloon
的自定义类型实现此接口,则只需首先声明该类将其实现如下即可:
public class Balloon : IEquatable<Balloon>
在类的内部,您必须实现Equals
方法。
现在请注意,由于此方法的签名如下:
bool Equals (T obj);
您必须更改为:
bool Equals(Balloon obj)
从现在开始, Balloon
是您的具体类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.