Restricting T to string and int?

2019-03-24 02:57发布

问题:

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?

回答1:

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?



回答2:

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



回答3:

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 { }


回答4:

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.



回答5:

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



回答6:

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.



回答7:

Simply use:

where TSource : IEquatable<string>