About 5,870,000 results
Open links in new tab
  1. c# - All possible array initialization syntaxes - Stack Overflow

    Feb 10, 2018 · As of C# 12, you can now also initialize arrays with collection expressions. Note the spread operator .., which will take an IEnumerable and flatten its items inside a collection expression. Some examples: Just a single element. int[] array = [1]; 100 elements where each element represents its index. int[] array = [.. Enumerable.Range(0, 100)];

  2. How do you initialize an array in C#? - Stack Overflow

    Aug 6, 2009 · Since C# 12, we can use collection expressions: // Create an array: int[] a = [1, 2, 3, 4, 5, 6, 7, 8]; // Create a jagged 2D array: int[][] twoD = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; // Create a jagged 2D array from variables: int[] row0 = [1, 2, 3]; int[] row1 = [4, 5, 6]; int[] row2 = [7, 8, 9]; int[][] twoDFromVariables = [row0, row1, row2];

  3. How do I initialize an empty array in C#? - Stack Overflow

    Jan 4, 2012 · Performance Rule CA1825: Avoid allocating zero-length arrays. Rule discription: Initializing a zero-length array leads to an unnecessary memory allocation. Instead, use the statically allocated empty array instance by calling the Array.Empty method. In your case: var a = Array.Empty<string>();

    Missing:

    • Infinity

    Must include:

  4. Working with Arrays in C#: Declaration, Initialization, and Usage

    6 days ago · What Are Arrays in C#? An array in C# is a fixed-size, zero-indexed collection of elements of the same data type. Being zero-indexed means the first element is accessed with index 0, the second with index 1, and so on. Arrays are reference types that inherit from the System.Array class, which provides various methods and properties to work with ...

  5. Array Initialization in C#: Detailed Guide (2025) - ByteHide

    Dec 21, 2023 · One of the simplest ways to answer the burning question, “how to initialize an array in C#”, involves providing initial values at the time of declaration. It’s equivalent to simultaneously making a shopping list and buying those items.

  6. Initializing an Array in C# - DEV Community

    May 14, 2024 · The aim here is to show you all the possible syntaxes to initialize array in C#. Below is the list of possible array declarations and initializations: string[] array = new string[2]; string[] array = new string[] { "A", "B" }; string[] array = { "A" , "B" }; string[] array = new[] { "A", "B" }; string[] array = ["A", "B"];

  7. C# Array: How To Declare, Initialize And Access An Array In C#?

    Apr 1, 2025 · Learn All About C# Array in This In-depth Tutorial. It Explains How To Declare, Initialize And Access Arrays Along with Types And Examples Of Arrays in C#.

  8. Different Ways to Initialize Arrays in C# - Code Maze

    Jul 13, 2022 · In this article, we have learned how to initialize arrays in C# using different techniques. Additionally, we’ve shown several tests to prove the size of our initialized arrays. Ready to take your skills to the next level?

  9. Quick Array Initialization in C# - Web Dev Tutor

    Jun 25, 2024 · In this blog post, we will explore various methods for quickly initializing arrays in C#. 1. Initializing Arrays with Specific Values. You can initialize an array with specific values using array initializers. Here's an example: int[] numbers = new int[] { 1, 2, 3, 4, 5 }; In this example, we create an integer array numbers and initialize it ...

  10. Mastering Array Initialization in C# - Web Dev Tutor

    Jun 25, 2024 · The most common way to initialize an array in C# is by specifying the size of the array and assigning values to each element individually. You can also initialize an array with values directly at the time of declaration using array initializer syntax. For multi-dimensional arrays, you can initialize them by specifying the values for each dimension.

Refresh