A Dictionary in Csharp is a generic class that allows the user to create a Key-Value pair defined as a single instance.
using System;
using System.Collections.Generic;
class Test
{
public static void Main()
{
// Creating a dictionary
// Initialization doesn't require the "new" part.
Dictionary<int, string> sub = new Dictionary<int, string>();
// Adding elements
sub.Add(1, "C#");
sub.Add(2, "Javascript");
sub.Add(3, "Dart");
// Displaying dictionary
foreach (var ele in sub){
Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
}
}
}Example with a custome state machine:
using System;
using System.Collections.Generic;
class Test
{
public static void Main()
{
// Creating a dictionary
Dictionary<System.Type, Characters> sub;
sub = new Dictionary<System.Type, Characters>
{
{ typeof(Mario), new Mario() }
};
Console.WriteLine(sub);
// Displaying dictionary
foreach (var ele in sub){
Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
}
}
}
public interface Characters
{
void Start();
}
public class Mario : Characters
{
public void Start()
{
}
}