C# Workshop – Collections

Standard

Collections

It’s possible to store multiple values of the same type, mentioned previously in the form of an Array, these are normally declared with a fixed size and denoted using square brackets and can be used to store a sequence of values of that type.

To use an Array in dotnetfiddle.net enter the following:

using System;

public class Demo
{	
	public static void Main()
	{
		string[] letters = { "A", "B", "C", "D", "E", "F" }; 
		foreach(string letter in letters)
		{
			Console.WriteLine(letter);	
		}
	}
}

It is also possible to loop through an Array by Index where the first element is zero.
To loop through an Array this way, enter dotnetfiddle.net the following:

using System;

public class Demo
{	
	public static void Main()
	{
		string[] letters = { "A", "B", "C", "D", "E", "F" }; 
		for(int index = 0; index < letters.Length; index++)
		{
			Console.WriteLine(letters[index]);	
		}
	}
}

Arrays are built into C# and can be utilised for many things but they have limitations that their size must be known in advance and often you won’t know what to store beforehand, there is another way to store multiple items is with a collection.

There’s another way of storing items of a type, this is known as a Collection and there are two main kinds which are List and Dictionary.

The first type of collection is List where it is a list of a particular item so if it’s a List of int this would be List<int> where the type of the list is within angle brackets, they are more flexible than an Array as it can have as many items as you want and Collections have their own namespace which is System.Collections.Generic.

To use a List of int in dotnetfiddle.net, enter the following:

using System;
using System.Collections.Generic;

public class Demo
{	
	public static void Main()
	{
		List<int> numbers = new List<int>();
		for(int index = 1; index <= 10; index++)
		{
			numbers.Add(index);
		}
		foreach(int number in numbers)
		{
			Console.WriteLine(number);
		}
	}
}

It’s also possible to use a List of string in dotnetfiddle.net, enter the following:

using System;
using System.Collections.Generic;

public class Demo
{	
	public static void Main()
	{
		List<string> letters = new List<string>() 
		{ "A", "B", "C", "D", "E", "F" };
		foreach(string letter in letters)
		{
			Console.WriteLine(letter);
		}
	}
}

In the first example, there is a List of numbers which is added to using the Add Method to add something to the List within a for loop. Then in the second foreach loop the items that were added are output. Then in the second example there is a List of letters which have been prepopulated, like the Array and then the contents are output from a foreach loop.

The second type of collection is Dictionary which is like List but has two parts, there is the Key which will identify something that has been added, and Value which is the item that’s been added and like List the Dictionary can have many types of Value but can also have types of Key but mainly string is the most commonly used type for a Key and you can get values of a type by their Key.

To use a Dictionary by string of string in dotnetfiddle.net, enter the following:

using System;
using System.Collections.Generic;

public class Demo
{	
	public static void Main()
	{
		Dictionary<string, string> colours = new Dictionary<string, string>(); 
		colours.Add("R", "Red");
		colours.Add("G", "Green");
		colours.Add("B", "Blue");
		foreach(string key in colours.Keys)
		{
			Console.WriteLine(colours[key]);
		}
	}
}

In the example, there’s a Dictionary of string where the Key is also a string, it has an Add Method with the Key and then the Value there there’s loop which uses the Keys Property which is a List of the Keys and then output the item using the Key similar to an Array by using the square brackets.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s