How to Create One-Dimensional Array in C#
In this tutorial, you will learn how to create a one-dimensional array in your program in C#. We all know that an array a collection of variables of the same type that are referred to by a common name. Meaning, it is a storage variable that has different values.
To declare an array in C#, have this syntax:
- data type[ ] array name = new data type[size];
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Console Application and name your project as ArraySample.
2. Now, put this code in your program.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ArraySample
- class Program
- public static void Main()
- int element = new int; //declaring one-dimensional array
- int i; // variable for for loop
- for (i = 0; i <= 9; i++) //starts i with 0, ends at 9, and will increment by 1
- element[i] = i; // the array will hold the value of variable i in the loop
- for (i = 0; i <= 9; i++) // declare again the for loop
- Console.WriteLine("element[" + i + "]: " +
- element[i]); //display the array element from 0-9
We declare the one-dimensional array with the named element as an Integer variable that holds an index of 10. We all know that an array starts at 0, so the value must be 0-9 only.
Next, we have declared a for loop that has variable i that starts from 0, ends at 9, and will increment by 1 during execution. Next, the array named element holds now the value of variable i in the loop.
We also called again the loop and display it using the Console.WriteLine function as we call the value of their indexes and element equal from 0 to 9.
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware