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?
As others have said, you cannot use type restrictions in this way. What you can do is to specialise from your base type as follows:
Also, as your base class is generic, you won't be able to use this to polymorphically. If you need to do this make StatisticItemHits implement an interface and use this to reference instances. Something like:
You cannot restrict it to string and int from the where clause. You can check it in the constructor, but that is probably not a good place to be checking. My approach would be to specialize the class and abstract the class creation into a (semi-)factory pattern:
This way you can limit the class's usage to just string and int internally inside your class.
The type restriction is meant to be used with Interfaces. Your sample suggests that you want to allow classes that inherit from int and string, which is kinda nonsense. I suggest you design an interface that contains the methods you'll be using in your generic class StatisticItemHits, and use that interface as restriction. However I don't really see your requirements here, maybe you could post some more details about your scenario?
Simply use:
Given that you only have two types here I would go down an OO route here instead and just have two classes for the two types.
Generics are best used where the circumstances in which they can be applied are, you know, generic. They're a lot less use in circumstances like this.
You can restrict to struct or class types only, and I do think that there need to be numeric or operator based restrictions (e.g. must support +=)
Int and string are really quite different, certainly more different than int and double. It wouldn't make much sense for a generic class to support the immutable reference type of string and the value type of int without also supporting other types more similar to either of them.
Not possible to do this with 'where'. You can't do an 'or'.