Description
In this tutorial I will show you how to creates lists and arrays and how to get the values within them. I will also show explain the difference between the 2 and when to use which one. The tutorial is meant for beginning programmers meaning it will only cover the very basics and keep the explanation and examples simple.Used program
Visual Studio 2015 – C# console applicationMaterial
This tutorial will teach you the following:• Creating and using a list
• Creating and using an array
Creating and using a list
A list in C# can store multiple values of a certain type, so when creating a list you must also declare of which type the list will be. Creating a list is slightly different from creating an integer or double, because you will first have to initialize the list (creating it) and only then you can add values to the list. Because you can continuously add values to a list after creating it the list does not have a fixed size (resizable), making it different from an array. In order to demonstrate how to create a list and add values to it I will create a list that adds each (string) input of the user to it.
There are really only 2 steps to adding data to a list, but getting data can be a bit trickier. You can get a value of a list by calling one of the positions (the first position starts at 0!), but if you want to get a specific value from the list you will have to loop through the list to find it. See the example for both ways to get data from a list.
Please keep in mind that when getting the “Count” of a list, it will return a value that is 1 higher than the last position in the list. This is because the counting in a list starts at 0, while the counting of an amount of things starts at 1.