7

In Java I am able to have a list of classes like:

List<Class>

But how do I do this in C#?

5
  • 1
    Do you actually mean a list of types, or a generic list? Commented Oct 3, 2013 at 16:24
  • var list = new List<ClassName>(); ?? Commented Oct 3, 2013 at 16:25
  • A list of Objects of calss Class or a list of actual Classes? Commented Oct 3, 2013 at 16:25
  • I'm pretty sure this question is asking for a .Net equivalent of java.lang.Class<T> Commented Oct 3, 2013 at 16:26
  • yes that right so I can store a variety of classes Commented Oct 3, 2013 at 16:30

3 Answers 3

15

Storing the class types

If you mean a list of actual classes, not instances of a class, then you can use Type instead of Class. Something like this if you have instances of each class:

List<Type> types = new List<Type>();
types.Add(someClassInstance.GetType());
types.Add(someOtherClassInstance.GetType());

Or this if you know the actual classes:

List<Type> types = new List<Type>();
types.Add(typeof(SomeClass));
types.Add(typeof(SomeOtherClass));

More on typeof(...) vs ...GetType() here

Instantiating the types

To actually instantiate a class given a classes Type you can use Activator or reflection. See this post for information on that. It can get a little complicated however when the compiler doesn't know about the constructors/parameters and such.

// Create an instance of types[0] using the default constructor
object newObject = Activator.CreateInstance(types[0]);

Or alternatively

// Get all public constructors for types[0]
var ctors = types[0].GetConstructors(BindingFlags.Public);

// Create a class of types[0] using the first constructor
var object = ctors[0].Invoke(new object[] { });
Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks for that, but how would I then instantiate an instance of one of the types added to the list?
@user1592512 That's not the question you asked.
@user1592512 updated with some info on instantiation.
I don't see that you can use GetType() on a non-instantiated class
@Allstar It looks like typeof(ClassName) will work.
1

The same basic syntax works in C#:

List<YourClass> list = new List<YourClass>();

This requires you to have using System.Collections.Generic; at the top of your file, which is the namespace which provides most of the generic collections.

If you are attempting to store a list of types, you can use List<System.Type>. Instances of those types could then be constructed via Activator.CreateInstance (which accepts the type) as needed.

2 Comments

Java 5 was released in September 2004. .NET 2.0 was released in November 2005.
@JonSkeet Fixed - was being dumb ;)
0
    // Create the list
    List<ClassName> classList = new List<ClassName>();

    // Create instance of your object
    ClassName cn = new ClassName();

    // Add the object to the list
    classList.Add(cn);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.