Fill 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>
- How to perform for loop through an array elements
- How to get the first element of an array
- How to get the last element of an array
- How to filter an array elements
- How to get the length of an array
- How to get value of a specified element of an array
- How to insert an element to an array
- How to iterate through an array
- How to get maximum and minimum values from an int array
- How to display a range of items from an array