c# - How to fill an array with a single value

Fill an Array with a single value
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can fill an Array instance with a single value. That means we will set the same value for an Array object’s all elements. But there is no built-in method in the Array class to fill an Array instance with the same value. So in this .net c# tutorial code example, we will use the Enumerable Repeat() and ToArray() methods to fill an Array object with a single value.

The Enumerable Repeat() method generates a sequence that contains one repeated value. The Enumerable Repeat() method has two parameters those are element and count. The element is the value to be repeated and the count is the number of times to repeat the value in the generated sequence.

The Enumerable Repeat() method returns an IEnumerable<T> that contains a repeated value. The Repeat() method throws ArgumentOutOfRangeException if the count is less than 0.

The Enumerable ToArray() method creates an array from an IEnumerable<T>. The Enumerable ToArray() method returns an Array that contains the elements from the input sequence. It throws ArgumentNullException if the source is null.

In this .net c# example code we also fill an Array object with a single value using a for loop. We loop through all the elements of an Array object and set the same value for all of them.

So finally, using the Enumerable class Repeat() and ToArray() methods the .net c# developers can fill an Array with a single value.
fill-array-with-a-single-value.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //initialize and fill a string array with word 'c#'
        string[] stringArray = Enumerable.Repeat("c#",5).ToArray();

        string[] anotherStringArray = new string[4];
        int[] intArray = new int[3];

        //fill a string array with word 'asp.net' using for loop
        for (int i = 0; i < anotherStringArray.Length;i++ )
        {
            anotherStringArray[i] = "asp.net";
        }

        //fill an int array with value '10' using for loop
        for (int i = 0; i < intArray.Length; i++)
        {
            intArray[i] = 10;
        }
        
        Label1.Text = "string array elements.........<br />";
        foreach(string s in stringArray)
        {
            Label1.Text += s + "<br />";
        }

        Label1.Text += "<br />another string array elements.........<br />";
        foreach (string s in anotherStringArray)
        {
            Label1.Text += s + "<br />";
        }

        Label1.Text += "<br />int array elements.........<br />";
        foreach (int i in intArray)
        {
            Label1.Text += i.ToString() + "<br />";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - fill array with a single value</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - fill array with a single value
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="fill array with a single value"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples