4
Answers

How can I instantiate an array of an object that must take parameters?

Richard

Richard

18y
2.2k
1

I've been developing in C# for quite a while and I'd say that I have a pretty decent grasp on most things C# and .NET related.  However, there is one issue I can't seem to solve.  I've tried to look through the Microsoft documentation (although not for a very thorough period of time) so I figured I'd just suck up my pride and ask my fellow coders.

My question-- How do I instantiate an array of an object (without using the .NET Array class) when the object has a constructor that must take parameters.

I know I'm not always the best at wording things so let me give an example.  Let's say class foo has the following declaration:

public class foo {
   ...
   
   //Constructor
   public foo(int i) {
    ...
  }

  ...
}

How can I declare an array of class as such:

foo[] f = new /*I don't know what to do here.*/;

Any help would be appreciated.  Thanks!


Answers (4)