If I have a generic class:
public class MyClass<T>
{
public T Value;
}
I want to instantiate several items such as…
new MyClass<string>
new MyClass<int>
…and add them to a collection. How do I define the collection so that it can hold a list of generic types? I then want to iterate through the collection at some point, and use the Value property. Possible?
Have your generic class inherit from a non-generic base, or implement a non-generic interface. Then you can have a collection of this type and cast within whatever code you use to access the collection’s contents.
Here’s an example.