Hey guys,

I was wondering, in Java the following is allowed:

Class[] classList = new Class[10];
classList[0] = System.class;

Can this be done in C#?

Thanks! :icon_smile:

9 Years
Discussion Span
Last Post by PoovenM

That's a type of reflection methods in JAVA, actually C# treats with System.Type not System.Class like

Type[] types = new Type[10];
types[0] = System.String;
types[1] = System.TextBox;

I was thinking I'd have to use the Type class but I wasn't sure how I'd get the 'type' of the class into the array. Thanks Remy!! :icon_smile: I was wondering, would the following situation be possible?

I have an object that can be one of several classes, however only a certain subset of these class are accpetable. I was thinking that the following might be possible:

Type[]  classList = ...
WTTag tag = ...
for (int k = 0; k < classList.Length; k++)
       if (tag is classList[k])

Would this be valid :?:


Any class, interface, struct, enum or delegate is Type.

Your code is wrong as 'is' take a type not an instance; like

if (tag is WTTag) .....

Your logic isn't right as classList carries instances from one type so loop won't be useful as classList[0] like classList[1] like classList[2] like....


I'm not sure I fully understand Ramy; so all my classes have a Type. This Type is unique to a class and is similar to the ClassName.class attribute in Java? Except that a Type is implicately accessed:

Type classType = MyClassName;

The as operator takes an object and determines if it is an instance of a particular class right? If this is the case then based on what you've said, when I stored the 'Type' of a class in the array I don't really store enough information about the class to do an is comparison? That is, in general the following will not work:

Type classType = System.String;
String hello;
if (hello is classType) ...

I actually get a compile error (in the first line) so I guess that answers my question. Thanks again hey!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.