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
}
}
your Cars
enum, is a nested type inside class Bar
. It is not a member property / method of Bar
.
Therefore, it is not possible.
Cars is just a nested type of Bar. When you create another class, which derives from Bar, lets call it Bar2
, you will not have access to Bar2.Cars
neither, since that type will not be created. Nested types are not instance members, and are thus not inherited.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With