So if there is an enum
property in a class
called Bar
, why can't I access the enum
property or any static
property of type
<T>
in this situation. I am implicitly declaring that <T>
is of type
Bar
. Just wanted to know if it's simply a limitation of Generics or the enum
type
itself.
public class Foo<T> where T : Bar
{
public Foo()
{
// This obviously works
var car = Bar.Cars.Honda;
var name = Bar.Name;
// Why can't I do this ?
var car2 = T.Cars.Toyota;
var name2 = T.Name;
}
}
public class Bar
{
public static string Name { get; set; }
public enum Cars
{
Honda,
Toyota
};
}
UPDATED
In @Frederik Gheysels's answer, it's mentioned that if I have a class
that is simply derived from Bar
that I wouldn't have access to the enum
or any static
of the base
. That is not correct, this compiles and works.
public class Foo : Bar
{
public Foo()
{
// This all works
var address = this.Address;
var car = Foo.Cars.Honda;
var name = Foo.Name;
}
}
public class Bar
{
public static string Name { get; set; }
public string Address { get; set; }
public enum Cars
{
Honda,
Toyota
}
}