- This topic shows how to add content of TextBox to Array or create an array of string in C#.
Array in C# – Add content of TextBox to Array in C#
- An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school.
- By using an array, you can refer to these related values by the same name, and use a number that’s called an index or subscript to tell them apart.
- The individual values are called the elements of the array.
- They’re contiguous from index 0 through the highest index value.
- In contrast to an array, a variable that contain a single value is called a scalar variable.
Zero Length Arrays – Add content of TextBox to Array in C#
- An array that contains no elements is also called a zero-length array.
- A variable that holds a zero-length array doesn’t have the value Nothing.
- To create an array that has no elements, declare one of the array’s dimensions to be -1, as the following example
|
1 |
string[,] twoDimensionalStrings = new string[-1 + 1, 4]; |
Size of an Array – Add content of TextBox to Array in C#
- The size of an array is the product of the lengths of all its dimensions.
- It represents the total number of elements currently contained in the array.
|
1 |
long[,,] prices = new long[4, 5, 6]; |
Add content of TextBox to Array in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnArray_Click(object sender, EventArgs e) { string[] strName = new string[5]; strName[0] = textBox1.Text.ToString(); strName[1] = textBox2.Text.ToString(); strName[2] = textBox3.Text.ToString(); strName[3] = textBox4.Text.ToString(); strName[4] = textBox5.Text.ToString(); label1.Text = strName[0]; label2.Text = strName[1]; label3.Text = strName[2]; label4.Text = strName[3]; label5.Text = strName[4]; } } } |

Add content of TextBox to Array in C# – design the frame as in the figure

Add content of TextBox to Array in C# – Final Result
Add content of TextBox to Array in C#






