I created an object array with different types of elements in them:
public static int a;
public static string b;
public static ushort c;
object[] myobj = new obj[]{ a, b, c};
If I want to create an array that contains elements of arrays of this myobj type, how would I do it?
I mean something like this:
myobj[] myarray = new myobj[]; <= but to do this, myobj should be a type.
Not sure how to work it out.
Thanks everyone.
How about we use a
Dictionaryto store any types you need?So, while you will not exactly have
myType.a, you can havemyType.Values["a"], which is close enough, makes use of standard C# constructs, and gives you lots of flexibility/maintainabilityAnd sample usage:
Plus, if you want, you can easily add an
indexerproperty to your class, so you can access elements with the syntaxmyType["a"]. Notice that you should add error checking when adding or retrieving values.And here’s a sample using indexer. Increment the entries by ‘1’ so we see a difference in the ouptut: