Restricting T to string and int?

2019-03-24 02:14发布

I have built myself a generic collection class which is defined like this.

public class StatisticItemHits<T>{...}

This class can be used with int and string values only. However this

public class StatisticItemHits<T> where T : string, int {...}

won't compile. What am I doing wrong?

7条回答
别忘想泡老子
2楼-- · 2019-03-24 03:10

You could make StatisticItemHits<T> an abstract class and create two subclasses:

StatisticItemHitsInt : StatisticItemHits<int>{}

StatisticItemHitsString : StatisticItemHits<string>{}

That way there can only be an int and string-representation of StatisticItemHits

查看更多
登录 后发表回答