Dan Kenny Game Design
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact
  • Home
  • Portfolio
    • Game Design
    • 2D & Animation
  • Blog
  • About Me
  • Contact

C# Beginners Tutorial: Arrays

8/4/2018

Comments

 
Hey there, Gamers and Game Makers!

In this week's blog, we're going to continue with our introduction to C# series by looking at Arrays. Let's get started.

So what exactly is an Array? Well, you can think of an Array as a type of variable but an Array contains essentially a list of information. So for example if you have an int Array then that Array will contain multiple int values.

We declare an Array in the following way:

You first declare the type of Array you want by declaring the variable type as normal. You then follow that with a pair of square brackets.We then follow this with the name you want to give your Array followed by an equals sign and then "new" followed by the variable type. Finally we finish with another pair of square brackets. This time however, we put the size of our Array inside these brackets. This defines how many places are in our Array.

I'm going to create an Array called names. So it'll look as follows:
Picture
Ok, so how do we go about adding values to the positions of our new Array? We can simply say the name of our Array followed by square brackets with the index position of our Array followed by an equals sign and finally followed by the new value. Keep in mind that an Array starts its index position at 0. So the first value of your Array will be at position 0.

If I want to add my name to the first position of the Array of names, it'll look as follows:
Picture
Ok, I'm going to assign values to the other positions of our Array so that it is full. Our script currently looks as follows:
Picture
Now that our Array is full, let's try printing the values from various positions in the Array. So if I want to print the name held at index position two, I'd type the following:
Picture
Picture
As you can see, when we run the script, the name John is printed out. This is because we are telling our script to print the name held at index position two. If you look at your initialization, you see that index position two is given the string value of "John" and that is what get's printed to the console.

If I change the index position to one, we see the name "Tim" being printed because that is the name held at that index position.

Picture
Picture
That does it for this quick introduction to Arrays. We can do so much more with Arrays so I encourage you to play around with them.

Until next time!

Comments

    Archives

    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    August 2014
    November 2013
    September 2013

    Categories

    All

    RSS Feed

© COPYRIGHT 2020. ALL RIGHTS RESERVED.