How to create generic class which takes 3 types.

Posted by scope-creep on Stack Overflow See other posts from Stack Overflow or by scope-creep
Published on 2010-05-12T20:30:47Z Indexed on 2010/05/12 20:34 UTC
Read the original article Hit count: 537

Filed under:
|

I'm trying to make a generic class that takes 3 types, either a simple string, IList or a IList.

public class OntologyStore
{

}


public sealed class jim<T> where T:new()
{
    IList<string> X = null;
    IList<OntologyStore> X1 = null;

    public bob()
    {
        if (typeof(T) == typeof(String))
        {
            X = new List<string>();
        }
        if (typeof(T) == typeof(OntologyStore))
        {
            X1 = new List<OntologyStore>();
        }
    }
}

I can easily create, which you would expect to work,

jim<OntologyStore> x1=new jim<jim<OntologyStore>()

as you would expect, but when I put in

jim<string> x2=new jim<string>()

the compiler reports the string is non abtract type, which you would expect.

Is it possible to create a generic class, which can instantiate as a class which holds string, or a IList or an IList?

© Stack Overflow or respective owner

Related posts about c#4.0

Related posts about generics