If all objects are from the same Base class then you can create an array of the Base class and stor the Child Class's objects in it. Like:
public void SomeBaseMethod()
class SomeChildClass1 : SomeBaseClass
public void SomeChildMethod1()
class SomeChildClass2 : SomeBaseClass
public void SomeChildMethod2()
To Store the objects of Child Classes in a single aray you can write something like this:
SomeChildClass1 someChildClass1 = new SomeChildClass1(); // Create an object of SomeChildClass1
SomeChildClass2 someChildClass1 = new SomeChildClass2(); // Create an object of SomeChildClass1
SomeBaseClass someBaseClassArr = new SomeBaseClass; // Create an Array to store both objects..
someBaseClassArr = someChildClass1; // Insert someChildClass1 at first Index
someBaseClassArr = someChildClass2; // Insert someChildClass2 at second Index
Retrival is a bit tricky and can be error prone if you make a mistake obseve this:
SomeChildClass1 someChild1TempObject = (SomeChildClass1 ) someBaseClassArr; // Because it was stored as Base Object so for putting it back to the original object you need to cast back it...
SomeChildClass2 someChild2TempObject = (SomeChildClass2 ) someBaseClassArr; // Because it was stored as Base Object so for putting it back to the original object you need to cast back it...
Both Statmenets will work fine:
How Ever the following line will create an Invalid Exception at Runtime:
SomeChildClass2 someChild1TempObject = (SomeChildClass2 ) someBaseClassArr; // Error! because you stored object of SomeChildClass1 at Index 0 so it cannot be converted to object of SomeChildClass2...
To Avoid this Exception We use as Keyword as follow:
SomeChildClass2 someChild1TempObject = someBaseClassArr as SomeChildClass2;
if(someChild1TempObject == null) // Means Casting was Invalid
else // Parsing was Successfull
Above Solution works smooothly without any Runtime Error
So The concept here is that you can put any object of child class in the object of BaseClass but for getiting that object back to original class's object you need to parse it back and you should use as keyword to work safely and knowing if the casting was OK or Not.
You were asking about others, Int, Float etc....
To store all Type of objects in a single Array you can use:
object objArr = new object;
You can story any type of object like int, SomeChildClass, SomeBaseClass, String, Float etc...... Because The base class of all types in .Net is the object class but you should check for safety when getting back the object to its real type.....
ArrayList also works on the same Rule, it stores all types as objects and you need to convert them back when you need any item back:
Here is an example:
ArrayList arrayList = new ArrayList();
arrayList.Add(1); //Store Int
arrayList.Add("Rizwan"); //Store string
arrayList.Add(someChildClass1); //Store object of someChildClass1
The benefit of using ArrayList Collection over Array is that it's size is Dynamic and it expands at runtime when you insert more objects in it.....
I hope this little tutorial for you goes in Right Direction to help you :).