What is the difference between this two ways of initializing arrays. I have instantiated an array Guys[] guy = new Guy[3]; where Guys is a class having fields Name, Cash, MyLabel(a Label), MyRadioButton(a RadioButton)

guys[0] = new Guy() 
                {Name = "Joe", Cash = 50, MyLabel = joeBetlabel, MyRadioButton = joeRadioButton };
            guys[1] = new Guy() 
                { Name = "Bob", Cash = 75, MyLabel = bobBetlabel, MyRadioButton = bobRadioButton };
            guys[2] = new Guy() 
                { Name = "Al", Cash = 45, MyRadioButton = alRadioButton, MyLabel = AlBetlabel };

the second method is

guys = new Guy[3]
                {
                    new Guy() {Name="Joe", Cash=50, MyRadioButton=rbGuy1, MyLabel=lblBetDesc1},
                    new Guy() {Name="Bob", Cash=75, MyRadioButton=rbGuy2, MyLabel=lblBetDesc2},
                    new Guy() {Name="Al", Cash=45, MyRadioButton=rbGuy3, MyLabel=lblBetDesc3}
                };



            What does each method actually mean especially in terms of using an array member in both cases

Recommended Answers

All 8 Replies

Nothing really, they both have the same outcome. The second way is the preferred method if you know the contents before hand.

It boils down to;

Guy[] guys = new Guy[3]; -- You're reserving space for 3 "Guy" objects in this array. You don't yet know what object is going where, this will be filled in later.
Essentially guys[0 to 2] == null

Guy[] guys =
    new Guy[3]
    {
        new Guy { Name="Joe", Cash=50, MyRadioButton=rbGuy1, MyLabel=lblBetDesc1 },
        new Guy { Name="Bob", Cash=75, MyRadioButton=rbGuy2, MyLabel=lblBetDesc2 },
        new Guy { Name="Al", Cash=45, MyRadioButton=rbGuy3, MyLabel=lblBetDesc3 }
    };

I'm initialising the array with these three objects. Seeing as we already know the contents, there's no point reserving space and then filling it in later.

If you were going to do your method 1, the compiler would probably optimise it away so that the array is initialised with the contents on the next three lines (although I can't guarantee).

So, use method 1 when you're filling array contents from a different source or you don't have the contents yet.
Use method 2 when you already know which objects are going to be used.

Not much difference I guess, I would use option 2.

I have another question here, how do I have all the array objects call one method e.g. UpdateLabels() which is non-static and public found in that class Guy? I'd like to know for both methods how I'd have to call that method. Thanks for the responses so far

To begin with I would not use an array here but a List. Like so:

List<Guy> guys = new List<Guy>{
        new Guy { Name="Joe", Cash=50, MyRadioButton=rbGuy1, MyLabel=lblBetDesc1 },
        new Guy { Name="Bob", Cash=75, MyRadioButton=rbGuy2, MyLabel=lblBetDesc2 },
        new Guy { Name="Al", Cash=45, MyRadioButton=rbGuy3, MyLabel=lblBetDesc3 }
    };

Then I would "walk" through the list with foreach and call an UpdateLabel method on every Guy object.

If you're using a List as ddanbe suggested, as an extra bonus you can use the ForEach method on the list object.

guys.ForEach(guy => guy.UpdateLabels());

There's little difference between foreach and List<T>.ForEach other than the latter is slightly faster and allows you to modify the collection (which you shouldn't do this way anyway).

This questions is an excerpt from Head First C# 2nd edition. Its stated than an array be used but I have had trouble calling a method using an array of objects. Somehow tbere is something I miss as not everything in tbe aarray is initialized. I need to know tha various ways of using an array of objects to call one method. The for and foreach loop almost worked great but I cant grasp the idea of using array of objects. Any feedback is higjly welcome and thanks all for ua responses.

I forgot to mention where exactly the excerpt id from. It's fron Lab 1 A Day at the Races. Am through with the book but somehow the logic behind the working of an array of objects calling the same method over and over again. Thanks

Then consider a guys[i] as an object.
In your code you initialized 3 object of type Guy.
guys[0], guys[1] and guys[2].
You can loop through them with a for-loop and set some properties or methods for every Guy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.