Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:08:12+00:00 2026-05-29T22:08:12+00:00

Given a class: class clsPerson { public int x, y; } Is there some

  • 0

Given a class:

class clsPerson { public int x, y; }

Is there some way to create an array of these classes with each element initialized to a (default) constructed instance, without doing it manually in a for loop like:

clsPerson[] objArr = new clsPerson[1000];

for (int i = 0; i < 1000; ++i)
    objArr[i] = new clsPerson();

Can I shorten the declaration and instantiation of an array of N objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:08:13+00:00Added an answer on May 29, 2026 at 10:08 pm

    You must invoke the constructor for each item. There is no way to allocate an array and invoke your class constructors on the items without constructing each item.

    You could shorten it (a tiny bit) from a loop using:

    clsPerson[] objArr = Enumerable.Range(0, 1000).Select(i => new clsPerson()).ToArray();
    

    Personally, I’d still allocate the array and loop through it (and/or move it into a helper routine), though, as it’s very clear and still fairly simple:

    clsPerson[] objArr = new clsPerson[1000];
    for (int i=0;i<1000;++i) 
       clsPerson[i] = new clsPerson(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that one can define a class in VB Script, is there any way
Given: class Hokey { public: explicit C(int i): i_(i) { } template<typename T> T&
I have class Foo with a constructor as given: class Foo { public: Foo(int
Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
Given class: public class FooAmounts int Id decimal Month1 decimal Month2 ... decimal Month12
Given: class A { public void m(List l) { ... } } Let's say
i have been given class with int variables x and y in private, and
Is there any possibility that GetPropInfo returns nil even if the given class is
Does anyone know a way to auto-generate database tables for a given class? I'm
My problem is thus: I need a way to ensure only one given class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.