How to store functions in Dictionary with uint as key (C#)
Can someone provide me an example of how maybe I store different开发者_运维百科 functions in a dictionary with int as a key and function as value. So then I could easly call function as following:
functionsDictionary[123](string);
Note all functions in dictionary will take only one input which is string. And will have no return.
It sounds like you're after
Dictionary<int, Action<string>>
or possibly (based on your title)
Dictionary<uint, Action<string>>
Sample:
using System;
using System.Collections.Generic;
class Test
{
static void Main()
{
var dictionary = new Dictionary<int, Action<string>>
{
{ 5, x => Console.WriteLine("Action for 5: {0}", x) },
{ 13, x => Console.WriteLine("Unlucky for some: {0}", x) }
};
dictionary[5]("Woot");
dictionary[13]("Not really");
// You can add later easily too
dictionary.Add(10, x => Console.WriteLine("Ten {0}", x));
dictionary[15] = x => Console.WriteLine("Fifteen {0}", x);
// Method group conversions work too
dictionary.Add(0, MethodTakingString);
}
static void MethodTakingString(string x)
{
}
}
Dictionary<int, Action<string>> _functions = new Dictionary<int, Action<string>>();
_functions[123]("hello");
精彩评论