How to Create One-Dimensional Array in C#

Submitted by: 
Language: 
Visitors have accessed this post 145 times.

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.