I have recently begun working with C# and there is something I used to do easily in Python that I would like to achieve in C#.
For example, I have a function like:
def my_func():
return "Do some awesome stuff"
And a dictionary like:
my_dic = {"do": my_func}
I have made a script in which when the user would input "do", the program would call my_func
according to the dictionary.
I'd like to know how I can assign functions to strings in a C# dictionary.
Basically in the same way:
static void Main() {
var dict = new Dictionary<string, Action>();
// map "do" to MyFunc
dict.Add("do", MyFunc);
// run "do"
dict["do"]();
}
static void MyFunc() {
Console.WriteLine("Some stuff");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With