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

  • SEARCH
  • Home
  • 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 548115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:02:42+00:00 2026-05-13T11:02:42+00:00

How do I initialize this array of custom types: PostType[] q = new PostType[qArray.Length];

  • 0

How do I initialize this array of custom types:

PostType[] q = new PostType[qArray.Length];

//initialize array
for( int x = 0; x < qArray.Length; x++)
    q[x] = new PostType();

Is there a better way to initialize this array?

  • 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-13T11:02:43+00:00Added an answer on May 13, 2026 at 11:02 am

    The way you are doing it is fine:

    PostType[] q = new PostType[qArray.Length];
    for (int i = 0; i < q.Length; i++)
        q[i] = new PostType();
    

    One thing I have changed are to rename the index veriable from x to i, as I find this easier to read, although it’s a subjective thing.

    Another thing I have changed is the for loop end condition should depend on the length of q, not on the length of qArray. The reason for this is that with your method if you decide to change the first line to use a different length instead of qArray.Length, you’d have to remember to change the second line too. With the modified code you only need to update the first line of code and the rest will work without modification.

    You could also do this using Linq:

    PostType[] q = Enumerable.Range(0, qArray.Length)
                             .Select(_ => new PostType())
                             .ToArray();
    

    But for large arrays this will be slower and not really easier to read in my opinion (especially if you haven’t seen it before). I think I’d probably just stick with the first method if I were you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I initialize an array this way: array = Array.new array << '1' << '2'
I have an array of int and i have to initialize this array with
To create and initialize an array with another array I currently do this: void
Is this: ByteBuffer buf = ByteBuffer.allocate(1000); ...the only way to initialize a ByteBuffer ?
Is there a way to construct a new std::vector with uninitialized (non-zero) values or
Whenever you allocate a new array in C# with new T[length] the array entries
For example: int main() { { // I want this array to be part
I would like to be sure that this is not wrong: I initialize array
Which is the best practice in this situation? I would like an un-initialized array
Suppose I have a class with a string instance attribute. Should I initialize this

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.