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 4329332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:48:34+00:00 2026-05-21T09:48:34+00:00

What are all the array initialization syntaxes that are possible with C#?

  • 0

What are all the array initialization syntaxes that are possible with C#?

  • 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-21T09:48:35+00:00Added an answer on May 21, 2026 at 9:48 am

    These are the current declaration and initialization methods for a simple array.

    string[] array = new string[2]; // creates array of length 2, default values
    string[] array = new string[] { "A", "B" }; // creates populated array of length 2
    string[] array = { "A" , "B" }; // creates populated array of length 2
    string[] array = new[] { "A", "B" }; // creates populated array of length 2
    string[] array = ["A", "B"]; // creates populated array of length 2
    

    Note that other techniques of obtaining arrays exist, such as the Linq ToArray() extensions on IEnumerable<T>.

    Also note that in the declarations above, the first two could replace the string[] on the left with var (C# 3+), as the information on the right is enough to infer the proper type. The third line must be written as displayed, as array initialization syntax alone is not enough to satisfy the compiler’s demands. The fourth could also use inference. The fifth line was introduced in C# 12 as collection expressions where the target type cannot be inferenced. It can also be used for spans and lists. If you’re into the whole brevity thing, the above could be written as

    var array = new string[2]; // creates array of length 2, default values
    var array = new string[] { "A", "B" }; // creates populated array of length 2
    string[] array = { "A" , "B" }; // creates populated array of length 2
    var array = new[] { "A", "B" }; // created populated array of length 2
    string[] array = ["A", "B"]; // creates populated array of length 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to get an array of all elements in an html
Is there a built-in method in Python to get an array of all a
All, I am trying to load up a bmp file into a GLubyte array
Is there a way to initialize all elements of an array to a constant
Imagine you have a large array of floating point numbers, of all kinds of
Is there a way to get all alphabetic chars (A-Z) in an array in
I'm trying to write a 2d array into an output file, it's all working
I have a string. I need to replace all instances of a given array
Is there a simple way to check if all values in array are equal
I've written a rather simple(ish) stack implementation that would automatically grow its internal array

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.