Skip to main content

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:

  1. 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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ArraySample
  7. {
  8. class Program
  9. {
  10. public static void Main()
  11. {
  12. int[] element = new int[10]; //declaring one-dimensional array
  13. int i; // variable for for loop
  14.  
  15. for (i = 0; i <= 9; i++) //starts i with 0, ends at 9, and will increment by 1
  16. element[i] = i; // the array will hold the value of variable i in the loop
  17.  
  18. for (i = 0; i <= 9; i++) // declare again the for loop
  19. Console.WriteLine("element[" + i + "]: " +
  20. element[i]); //display the array element from 0-9
  21. }
  22. }
  23. }

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.

Output:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

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.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.