开发者

C#: Generics syntax question

开发者 https://www.devze.com 2022-12-08 12:15 出处:网络
What\'s the syntax to require T also be IComparable 开发者_如何学Cin this class definition? public class EditItems<T> : Form

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

精彩评论

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