Hack Forums
[C#] C# Arrays [Intermediate Level Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# Arrays [Intermediate Level Tutorial] (/showthread.php?tid=756368)



C# Arrays [Intermediate Level Tutorial] - Pulser - Oct 21, 2010

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]);
            }

        }



* - tflo - Oct 21, 2010

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?


* - HazXoD3D - Oct 21, 2010

Another way to explain an array is using a chart:

Index | Value
0 | it
1 | is
2 | simple
3 | to
4 | understand


* - Pulser - Oct 21, 2010

(Oct 21, 2010 02:03 PM)Vanquish 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.


* - xhtmlphp™ - Oct 21, 2010

I prefer using
Code
string[] i = new string[] {"one", "two", "three"};

But the tutorial is great ;)


* - Pulser - Oct 21, 2010

(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