Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to instantiate a number of objects related to a particular class. I am using an array to handle this approach -
C#
Task[] task = new Task[4];

and then
C#
for (int i = 0; i <= 3; i++)
{
   task[i] = new Task();
}

However I am not certain if this is the right approach or if there is a more efficient method to handle these different objects.

Thanks
Posted
Updated 6-Sep-11 9:45am
v2
Comments
Wendelius 6-Sep-11 15:45pm    
Pre tags added
Philippe Mori 6-Sep-11 19:45pm    
As shown in solution 2, if you uses a loop for initialization, it is better to uses the Length property for more maintanable code (avoid cascvading changes if the number of task changes).

If you know how many objects you will have, arrays are great and you will not find anything faster.

If you have an unknown number of objects and need to access them ordered, use List<T> (internally a List uses an array).

There are many other collections, but I must know a little more to explain if any other collection is better than an array or a list for your case.
 
Share this answer
 
That should work fine. Of course there are lot's of alternatives like:
If the amount of tasks is always a constant:
C#
Task[] task = new Task[4]{ new Task(), new Task(), new Task(), new Task() };

If the amount in the declaration can change:
C#
for (int i = 0; i < task.Length; i++)
{
   task[i] = new Task();
}

and so on..

As for storing you can use array which is lightweight and fast but if you want more flexibility and programmability against the collection then you could consider something from System.Collections.Generic Namespace[^]
 
Share this answer
 
Comments
Philippe Mori 6-Sep-11 19:44pm    
If the first form is used, then specifying the size is not required...
Wendelius 7-Sep-11 1:13am    
Yes, that's quite true. Personally I prefer to write it since it's easier to to look at the size of an array in definition than to count the number of elements :) Thanks for your comment.
Sergey Alexandrovich Kryukov 7-Sep-11 0:23am    
Good point, my 5.
Important to note: OP's use of immediate constants 3 and 4 is a nasty mistake. It will work in this case but impossible to support.
--SA
Wendelius 7-Sep-11 1:14am    
Thank you SA
tamasu 8-Sep-11 11:34am    
Thanks. My issue is that I need to initially populate the list dynamically, that is the list depends on the user's input. Once the items are defined, they need to be accessed throughout the project.

For instance I have declared an array as: <pre lang="c#">Task[] task = new Task[4]; </pre>. The class Task contains more then one property which are all initially defined by the user. Lets consider that Task 1 was set as "not configured" with its property "status" set as true.

I am not concerned about structuring the get and set but how the same array of objects (in this case 4) is accessed by the other objects and therefore without another instantiation.

Thanks again for your assistance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900