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 02:46

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 { }
查看更多
Summer. ? 凉城
3楼-- · 2019-03-24 02:53

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-03-24 02:54

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?

查看更多
Emotional °昔
5楼-- · 2019-03-24 02:58

Simply use:

where TSource : IEquatable<string>
查看更多
smile是对你的礼貌
6楼-- · 2019-03-24 03:07

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.

查看更多
Bombasti
7楼-- · 2019-03-24 03:09

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

查看更多
登录 后发表回答