Information Technology Reference
In-Depth Information
class Program
{
static void Main()
{
Animal[] AnimalArray = new Animal[3];// Create Animal array
AnimalArray[0] = new Cat(); // Insert Cat class object
AnimalArray[1] = new Bird(); // Insert Bird class object
AnimalArray[2] = new Dog(); // Insert Dog class object
foreach( Animal a in AnimalArray ) // Cycle through array
{
ILiveBirth b = a as ILiveBirth;// if implements ILiveBirth...
if (b != null)
Console.WriteLine("Baby is called: {0}", b.BabyCalled());
}
}
}
This code produces the following output:
Baby is called: kitten
Baby is called: puppy
Figure 17-11 illustrates the array and the objects in memory.
Figure 17-11. Different object types of base class Animal are interspersed in the array.
Search WWH ::




Custom Search