   C# Object Arrays
  I am trying to work with an array of class objects and I'm having trouble getting it to work. When I run the code below I get an exception telling me I am trying to initialize a null value and to use the "new" keyword.I don't get it. Didn't I just do that?Thanksnamespace WindowsFormsApplication1{    public partial class Form1 : Form    {   class Dog   {  public string type;  Dog()  { type = "";  }   };   public Form1()   {  InitializeComponent();  getDogs();   }   void getDogs()   {  Dog[] myDogs;  myDogs = new Dog[3];  myDogs[0].type = "Husky";  myDogs[1].type = "Labrador";  myDogs[2].type = "Doberman";   }    }
  What you did was to allocate an array of 3 elements. Then you need to allocate each element.myDogs[0] = new Dog()myDogs[1] = new Dog()myDogs[2] = new Dog();  Though a better solution might be to use a collection and put the name in the constructorpublic class Dog{    public string type { get; set; }    public Dog()    {   this.type = "";    }    public Dog(string type)    {   this.type = type;    }}List<Dog> dogs = new List<Dog>();dogs.Add(new Dog("Husky"));dogs.Add(new Dog("Labrador"));dogs.Add(new Dog("Doberman"));;  In C# 3.0, you can also use an allocator listclass Dog{   public string Name {get; set;}}Dog[] dogs = new Dog[]{    new Dog() { Name = "Rex" },    new Dog() { Name = "Rover" },    new Dog() { Name = "Bob" },    new Dog() { Name = "Dave" },};