• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create One-Dimensional Array in C#

jakobovis

Absurdity Curator
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
150
Likes
128
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. namespace

    ArraySample
  6. {
  7. class

    Program
  8. {
  9. public

    static

    void

    Main(

    )
  10. {
  11. int

    [

    ]

    element =

    new

    int

    [

    10

    ]

    ;

    //declaring one-dimensional array
  12. int

    i;

    // variable for for loop

  13. for

    (

    i =

    0

    ;

    i <=

    9

    ;

    i++

    )

    //starts i with 0, ends at 9, and will increment by 1
  14. element[

    i]

    =

    i;

    // the array will hold the value of variable i in the loop

  15. for

    (

    i =

    0

    ;

    i <=

    9

    ;

    i++

    )

    // declare again the for loop
  16. Console.

    WriteLine

    (

    "element["

    +

    i +

    "]: "

    +
  17. element[

    i]

    )

    ;

    //display the array element from 0-9
  18. }
  19. }
  20. }

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:
arrayoutput.png


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

327,515

327,523

Top