Las*_*sus 16 c# generics casting operator-overloading
只是想知道是否还有代表C#3.5中的以下代码:
public struct Foo<T> {
public Foo(T item) {
this.Item = item;
}
public T Item { get; set; }
public static explicit operator Foo<U> ( Foo<T> a )
where U : T {
return new Foo<U>((U)a.Item)
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
Jon*_*eet 20
转换运算符不能是通用的.从规范部分10.10开始,这里是转换操作符声明符的格式:
conversion-operator-declarator:
implicit operator type ( type identifier )
explicit operator type ( type identifier )
比较一下,比较一个方法标题:
method-header: attributes opt method-modifiers opt partial opt return-type member-name type-parameter-list opt(formal-parameter-list opt) type-parameter-constraints-clauses opt
(抱歉格式化 - 不知道如何做得更好.)
请注意,运算符格式不包括类型参数列表或类型参数约束.