Friday, July 31, 2009

How do you use a 2D array of classes in C#?

First create the array:


Dog[,] arrayname = new Dog[5, 3];





Initialize however much of it needs to be initialized, remember the first line gave you only a bunch of null references:





arrayname[0, 0] = new Dog();


arrayname[2, 1] = new Dog();


arrayname[3, 2] = new Dog();





Use it:


arrayname[2, 1].Eat();

statice

No comments:

Post a Comment