从泛型基类继承,应用约束,并在 C# 中实现接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2007429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inherit from a generic base class, apply a constraint, and implement an interface in C#
提问by Dan Rigby
This is a syntax question. I have a generic class which is inheriting from a generic base class and is applying a constraint to one of the type parameters. I also want the derived class to implement an interface. For the life of me, I cannot seem to figure out the correct syntax.
这是一个语法问题。我有一个从泛型基类继承的泛型类,并且正在对类型参数之一应用约束。我还希望派生类实现一个接口。对于我的生活,我似乎无法弄清楚正确的语法。
This is what I have:
这就是我所拥有的:
DerivedFoo<T1,T2> : ParentFoo<T1, T2> where T2 : IBar { ... }
The first thing that came to mind was this:
想到的第一件事是这样的:
DerivedFoo<T1,T2> : ParentFoo<T1, T2> where T2 : IBar, IFoo { ... }
But that is incorrect as that causes T2 to need to implement both IBar and IFoo, not DerivedFoo to implement IFoo.
但这是不正确的,因为这会导致 T2 需要同时实现 IBar 和 IFoo,而不是 DerivedFoo 来实现 IFoo。
I've tried a bit of Googling, use of colons, semicolons, etc, but I've turned up short. I'm sure the answer is head slappingly simple.
我尝试了一些谷歌搜索,使用冒号、分号等,但我发现很短。我相信答案非常简单。
采纳答案by Adam Robinson
You include the entire signature of your class before you define generic constraints.
在定义泛型约束之前,您需要包含类的整个签名。
class DerivedFoo<T1, T2> : ParentFoo<T1, T2>, IFoo where T2 : IBar
{
...
}
回答by Stan R.
public interface IFoo {}
public interface IBar {}
public class ParentFoo<T,T1> { }
public class DerivedFoo<T, T1> : ParentFoo<T, T1>, IFoo where T1 : IBar { }
回答by Eric Lippert
My recommendation: when you have a question about the syntax of the C# language, read the specification; that's why we publish it. You'll want to read section 10.1.
我的建议:当您对 C# 语言的语法有疑问时,请阅读规范;这就是我们发布它的原因。您需要阅读第 10.1 节。
To answer your specific question, the order of things in a class declaration is:
要回答您的具体问题,类声明中的顺序是:
- attributes, in square brackets
- modifiers ("public", "static", and so on)
- "partial"
- "class"
- the class name
- a comma-separated list of type parameter declarations inside angle brackets
- a colon followed a comma-separated list of base types (base class and implemented interfaces, base class must go first if there is one)
- type parameter constraints
- the body of the class, surrounded by braces
- a semicolon
- 属性,在方括号中
- 修饰符(“public”、“static”等)
- “部分的”
- “班级”
- 班级名称
- 尖括号内的类型参数声明的逗号分隔列表
- 冒号后跟以逗号分隔的基类型列表(基类和已实现的接口,如果有基类,则必须先行)
- 类型参数约束
- 类的主体,被大括号包围
- 分号
Everything on that list is optional except for "class", the name, and the body, but everything must appear in that order if it appears.
该列表中的所有内容都是可选的,除了“类”、名称和正文,但如果出现,则所有内容都必须按该顺序出现。
回答by user875234
public class KeyAndValue<T>
{
public string Key { get; set; }
public virtual T Value { get; set; }
}
public class KeyAndValue : KeyAndValue<string>
{
public override string Value { get; set; }
}
This is an extension off the existing answers. It defaults to string
if you don't supply a type. I didn't implement an interface but that shouldn't require anything different than usual.
这是现有答案的扩展。string
如果您不提供类型,则默认为。我没有实现接口,但这不应该需要与平常不同的任何东西。