Initializing an array filled with a repeated non-default value

suggest change

As we know we can declare an array with default values:

int[] arr = new int[10];

This will create an array of 10 integers with each element of the array having value 0 (the default value of type int).

To create an array initialized with a non-default value, we can use Enumerable.Repeat from the System.Linq Namespace:

  1. To create a bool array of size 10 filled with “true”
    bool[] booleanArray = Enumerable.Repeat(true, 10).ToArray();
  2. To create an int array of size 5 filled with “100”
    int[] intArray = Enumerable.Repeat(100, 5).ToArray();
  3. To create a string array of size 5 filled with “C#”
    string[] strArray = Enumerable.Repeat("C#", 5).ToArray();

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Arrays:
* Arrays
* Initializing an array filled with a repeated non-default value

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script