C# - Create Array with non-default values

To create an array with non-default values in C#, you have a few options depending on the data type of the array elements:

1. Initialize the array with explicit values during declaration:
You can specify the initial values of the array elements at the time of array declaration using an initializer list enclosed in curly braces '{}'.


// Example: Integer array with specific values
int[] numbers = { 10, 20, 30, 40, 50 };

2. Use array initializer with new keyword:
You can use the new keyword to create an array and provide specific values at the same time.


// Example: String array with specific values
string[] names = new string[] { "Alice", "Bob", "Charlie" };

3. Assign values to individual array elements after declaration:
If you have already declared the array without initializing it, you can assign values to individual array elements using the index.


// Example: Double array with specific values
double[] grades = new double[4]; // Declare an empty array with size 4

grades[0] = 85.5;
grades[1] = 90.0;
grades[2] = 78.3;
grades[3] = 95.2;

4. Use Array initialization methods:
For certain data types, you can use built-in array initialization methods like 'Array.Fill()' to assign the same value to all elements of the array.


// Example: Boolean array with a specific value (using Array.Fill)
bool[] flags = new bool[5];
Array.Fill(flags, true); // All elements will be set to 'true'

5. Using LINQ and collections:
If you want to initialize an array with specific values based on some logic or from a collection, you can use LINQ methods like 'ToArray()' or directly convert a collection to an array.


// Example: Convert a List to an int array
List list = new List { 1, 2, 3, 4, 5 };
int[] array = list.ToArray();

6. Using Enumerable.Repeat:
You can create an array with specific non-default values using 'Enumerable.Repeat' from the System.Linq namespace in C#. The 'Enumerable.Repeat' method allows you to repeat a specified value a specified number of times, effectively initializing an array with those repeated values.

Here's an example of how to create an array using Enumerable.Repeat:


using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        // Example: Create an int array with specific non-default values using Enumerable.Repeat
        int[] numbers = Enumerable.Repeat(42, 5).ToArray();

        // Print the elements of the array
        foreach (int num in numbers)
        {
            Console.WriteLine(num);
        }
    }
}

In this example, we use 'Enumerable.Repeat(42, 5)' to create an int array with five elements, all having the value 42. The 'ToArray()' method converts the 'IEnumerable' returned by 'Enumerable.Repeat' into an array.

Output:


42
42
42
42
42

'Enumerable.Repeat' is useful when you want to initialize an array with a specific value repeated multiple times, without explicitly writing a loop or using other array initialization methods. It provides a concise way to create arrays with repeated values.

By using these methods, you can create arrays with non-default values, making it easy to work with pre-defined data sets in your C# programs. Choose the method that best suits your specific use case and data initialization requirements.