Problem with Generics

Jun 10 2009 10:15 PM
OK, i just thought i would simplify my question a bit:

The following won't compile (missing a cast)
 class Program
{
static void Main(string[] args)
{
foo f = foo.CreateMe();
}
}

class foo : GenericTest<bar>
{
}
class bar
{
}

class GenericTest<T> where T:new() { public static GenericTest<T> CreateMe()
{
return new GenericTest<T>();
}
}


The following, compiles but has a run time error:
 class Program
{
static void Main(string[] args)
{
foo f = (foo)foo.CreateMe();
}
}

class foo : GenericTest<bar>
 {
}

class bar
 {
}
class GenericTest<T>
 { public static GenericTest<T> CreateMe()
{
return new GenericTest<T>();
}
}


Answers (1)