In this article I will present some good and worthy examples about the usage of Lists in C#.Net. There are quite a lot good articles for lists and their usage, this one will try to summarize their examples and and give the basics in one place.
The following examples, for working with Lists are considered valuable by me:
1. Create a list:
1 2 3 4 5 6 7 8 9 |
static void Main() { List<string> myList = new List<string>(); myList.Add ("vitoshacademy"); myList.Add (".com"); myList.Add ("is"); myList.Add ("the address"); myList.Add ("of this web site"); } |
2. Access your list with a for each and for loop:
1 2 3 4 5 6 7 8 9 |
foreach (string item in myList) { Console.WriteLine(item); } for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); } |
3. If you want to use some of the array functions, you may easily convert the list to an array:
1 2 |
string myLine = string.Join(" ", myList.ToArray()); Console.WriteLine(myLine); |
4. If you want to add the dictionary Keys or Values to list, you may do it this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var myDictionary = new Dictionary<int, string>(); myDictionary.Add(1, "A"); myDictionary.Add(2, "B"); myDictionary.Add(3, "C"); myDictionary.Add(4, "D"); myDictionary.Add(5, "E"); List<int> myKeys = new List<int>(myDictionary.Keys); List<string> myValues = new List<string>(myDictionary.Values); foreach (int myKey in myKeys) { Console.WriteLine(myKey); } |
5. And if you want to print the dictionary values and keys, without using lists:
1 2 3 4 |
foreach (var myDictCont in myDictionary) { Console.WriteLine("{0} => {1}", myDictCont.Key, myDictCont.Value); } |
6. Lists can be also reversed:
1 2 3 4 5 |
myList.Reverse(); foreach (string item in myList) { Console.WriteLine(item); } |
7. Another option is to make a list from a list, using just specific indices of the list (that is what I call a sentence!):
1 2 3 4 5 6 |
Console.WriteLine("Get Range Example:"); List<string> myNewListRange = myList.GetRange(3, 4); foreach (string item in myNewListRange) { Console.WriteLine(item); } |
Pretty much, that is all that I needed to remember about lists in C#. If you want to read more, you may do it here.
The united code from the examples is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ListsExample { class Program { static void Main() { List<string> myList = new List<string>(); myList.Add("Vitoshacademy.com"); myList.Add("is"); myList.Add("the address"); myList.Add("of this web site."); myList.Add("Go"); myList.Add("and"); myList.Add("check it!"); foreach (string item in myList) { Console.WriteLine(item); } for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); } string myLine = string.Join(" ", myList.ToArray()); Console.WriteLine(myLine); var myDictionary = new Dictionary<int, string>(); myDictionary.Add(1, "A"); myDictionary.Add(2, "B"); myDictionary.Add(3, "C"); myDictionary.Add(4, "D"); myDictionary.Add(5, "E"); List<int> myKeys = new List<int>(myDictionary.Keys); List<string> myValues = new List<string>(myDictionary.Values); foreach (int myKey in myKeys) { Console.WriteLine(myKey); } Console.WriteLine(); foreach (var myDictCont in myDictionary) { Console.WriteLine("{0} => {1}", myDictCont.Key, myDictCont.Value); } Console.WriteLine(); myList.Reverse(); foreach (string item in myList) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("Get Range Example:"); List<string> myNewListRange = myList.GetRange(3, 4); foreach (string item in myNewListRange) { Console.WriteLine(item); } Console.WriteLine(myList.Count); myList.Clear(); Console.WriteLine(myList.Count); } } } |