views:

415

answers:

6

I have build 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?

+1  A: 

Not possible to do this with 'where'. You can't do an 'or'.

tuinstoel
+9  A: 

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?

driAn
Yes, after taking a second look at the design, you are completely right about about your approach. Thanks for your help.
Matthias
+1  A: 

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:

class MyRestrictedGeneric<T>
{
    protected MyRestrictedGeneric() { }


    // Create the right class depending on type T
    public static MyRestrictedGeneric<T> Create<T>()
    {
        if (typeof(T) == typeof(string))
            return new StringImpl() as MyRestrictedGeneric<T>;

        if (typeof(T) == typeof(int))
            return new IntImpl() as MyRestrictedGeneric<T>;

        throw new InvalidOperationException("Type not supported");
    }


    // The specialized implementation are protected away
    protected class StringImpl : MyRestrictedGeneric<string> { }
    protected class IntImpl : MyRestrictedGeneric<int> { }
}

This way you can limit the class's usage to just string and int internally inside your class.

chakrit
string is not a value type..
driAn
string is a reference type, so it doesn't help (I guess)
tuinstoel
Why does the Create method accept an arg that is never used?
configurator
ah.. I was in a rush.. that's fixed.
chakrit
+5  A: 

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

Ruben
+3  A: 

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:

public class StatisticItemHits <T> { }

public class StringStatisticItemHits : StatisticItemHits<string> { }

public class IntegerStatisticItemHits : StatisticItemHits<int> { }

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:

public class StatisticItemHits <T> : IStaticticItemHits { }

public interface IStatisticItemHits { }
Sean Kearon
+2  A: 

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.

Keith