C# abstract class, works with array initialization

abstract-class, c#, design-patterns, oop

Solution

The values stored in the array during the initialization will all be `null`, so this doesn't actually create any instance of the abstract class. Array initialization corresponds to the following (correct) line:

Creator creator = null;

Creating arrays of a type `AbstractClass[]` is actually quite useful, because you can then store references to some concrete (inherited) class in the array. For example:

var objects = new object[2];
objects[0] = "Hello";
objects[1] = new System.Random();

Then you can for example iterate over the `objects` array and call `ToString()` on all the objects.

Problem

As we know that we CANNOT create the instance of `abstract class`. I just want to know that if we create the array of abstract class, it will sure work. E.g. ``` public abstract class Creator { public abstract void DoSomething(); } Creator creator = new Creator(); // this will give you compilation error! Creator[] creator = new Creator[2]; // this will SURE work and will NOT give you compilation error. ``` Can anybody please let me know that why this is happening and why it is working with array initialization? Thanks in advance.

Original source