Introduction
In this article, you will learn how to initialize a string array with default values using the new keyword and a specified size for the Array. An array is a collection of elements of the same type that can be accessed using an index. In the case of a string array, each element is a string value. When you create a new array in C#, it is initially filled with default values. For a string array, the default value is null. However, in some cases, you may want to initialize a string array with a specific default value instead of null. This can be useful, for example, when creating a new array to hold user input or when you want to ensure that all elements of the Array have a specific value to start with.
Several ways to initialize a string array with default values in C# include using the new keyword or the Enumerable.Repeat method. Initializing a string array with default values using the new keyword
The most basic way to initialize a string array with default values is to use the new keyword and specify the size of the Array. This will create a new string array with all elements set to null by default.
string[] myArray = new string[5];
This creates a string array with five elements; each initialized to null. You can access individual elements of the Array using an index like this.
string firstElement = myArray[0];
Initializing a string array with a specific default value using the Enumerable.Repeat method If you want to initialize a string array with a particular default value instead of null, you can use the Enumerable.repeat method to create a sequence of values and then convert it to an array using the Array () method.
Method 1. Enumerable.Repeat in C#
This method uses the Enumerable.Repeat the() method to the sequence of a repeated value and then convert the sequence back to the Array using the ToArray() method. This is Defer's execution in the implementation. The instant return value is an object with all the data needed to complete the action. The GetEnumerator method of the object, or for each in C#, can be used to enumerate the object before running the query represented by this method in C#.
The following code example shows the usage of this method.
public class Program
{
public static void Main()
{
int element = 2;
int count = 20;
int[] array = Enumerable.Repeat(element, count).ToArray();
Console.WriteLine(String.Join(",", array));
}
}
This C# program initializes an integer variable element to 2, and an integer variable count to 20. It then creates an integer array with a length of count. Next, it uses a for loop to iterate through each element of the Array, setting each element to the value of the element (which is 2 in this case).
Finally, it prints out the contents of the Array using the String.Join method to concatenate the elements of the Array into a comma-separated string.
Output

Method 2. Loop in C#
A for-loop is the recommended method for initializing an array. The code below shows how to use a for-loop to fill an array with an initial value in C#. It's common to want to run a particular block of statements a given number of times. Typing those statements the required number of times is one potential solution. However, the number of repetitions might not be known beforehand (during build time) or insufficient (say 100000) in C#.
The loop is the ideal solution to this issue. Programmers use loops to run a particular block of statements until a certain condition is.
public class Program
{
public static void Main()
{
int element = 2;
int count = 20;
int[] array = new int[count];
for (int i = 0; i < count; i++)
{
array[i] = element;
}
Console.WriteLine(String.Join(",", array));
}
}
This is a C# program that creates an array of integers, initializes each element to the value of 2, and then prints out the Array using the Console.WriteLine method. Here's a brief explanation of what each line of the code does
1. The line public class Program declares a public class named "Program."
2. The line public static void Main() declares a public static method named "Main," which is the program's entry point.


Prathap ReddyPosted May 4, 2023, 5:48 AM
Nice article naresh