开发者

C#: Generics syntax question

What's the syntax to require T also be IComparable 开发者_如何学Cin this class definition?

public class EditItems<T> : Form


You can use just where T : IComparable as shown by other answers. I find it's typically more helpful to constrain it with:

public class EditItems<T> : Form where T : IComparable<T>

That says it has to be a type which is comparable with itself.

For one thing, for value types this avoids boxing. For another, it means you're less likely to try to compare two values which aren't really comparable.


public class EditItems<T> : Form where T : IComparable


public class EditItems<T> : Form where T : IComparable


Use a type constraint (see MSDN):

public class EditItems<T> : Form where T : IComparable


public class EditItems<T> : Form where T : IComparable
{...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜