简体   繁体   中英

C#: Generics syntax question

在此类定义中要求T也必须IComparable的语法是什么?

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

使用类型约束 (请参见MSDN ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM