Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Arrays [Intermediate Level Tutorial]

C# Arrays


What Is It:
After reading my previous tutorial on variables I'm sure that you noticed your variable was only holding 1 thing. An array is a way of holding more than 1 variable at a time.


Getting Started:
To begin creating an array in C# is a lot similar to creating a normal variable

Code
string[] hf = new string[3];

String[] tells us the variable type and that it is going to be an array.

hf is the name.

= new string creates a new object, as well as states the variable type again.

[3] is the size of our variable



Now, we need to begin assigning text to your array. To do this we do the following:
Code
hf[0] = "hi";
hf[1] = "hello";
hf[2] = "sup";

[0]
[1]
[2]
These numbers are the position in your array. The array will begin at 0. After the equals sign is simply the text.



Code
string[] hf = new string[3];
            hf[0] = "hi";
            hf[1] = "hello";
            hf[2] = "sup";

Looping Through The Array
Okay, so we got our array setup. Now we want to loop through it an display the text in a messageBox 1 by 1.

Code
for (int i = 0; i != (hf.Length); i++)
            {
                MessageBox.Show(hf[i]);
            }

for (int i = 0; i != (hf.Length); i++)

This starts the for loop, it creates a new integer variable called "i", and it sets it equal to 0. Next, it says that as long as our variable "i" does not equal the length of our array, it will add 1.


{
MessageBox.Show(hf[i]);
}

This obviously displays a messagebox, the messagebox will show the text being stored in the array, each time the loop goes around it will add 1. [i] will represent the position in the array, determining what gets popped up.

Conclusion:
Arrays can be used to store many things, it gets rid of the need of having dozens of separate variables.

Code:
Code
{
            string[] hf = new string[3];
            hf[0] = "hi";
            hf[1] = "hello";
            hf[2] = "sup";

            for (int i = 0; i != (hf.Length); i++)
            {
                MessageBox.Show(hf[i]);
            }

        }
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
I would suggest using a foreach loop while dealing with arrays. Other than that good job.

Edit: One more thing isn't it Count instead of Length?
Another way to explain an array is using a chart:

Index | Value
0 | it
1 | is
2 | simple
3 | to
4 | understand
(Oct 21, 2010 02:03 PM)tflo Wrote:  I would suggest using a foreach loop while dealing with arrays. Other than that good job.

Edit: One more thing isn't it Count instead of Length?

Thanks for the feedback! Also, length seemed to work fine for me.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
I prefer using
Code
string[] i = new string[] {"one", "two", "three"};

But the tutorial is great ;)
(Oct 21, 2010 07:32 PM)xhtmlphp™ Wrote:  I prefer using
Code
string[] i = new string[] {"one", "two", "three"};

But the tutorial is great ;)

Yeah, that seems to be a great way to do it if you are using a small amount of things.

Thanks a lot for the comment :D
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by pzychonautJun 8, 2023 05:03 PM 5 177 Jun 8, 2023 05:03 PM
Last Post: padrino
[C] by EmacsSucksButILikeItJun 4, 2022 11:03 PM 1 222 Jun 4, 2022 11:03 PM
Last Post: unazed
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros