+ 2

[C#] How do I add a dictionary of methods?

I want to add a dictionary that an int keyed to methods, but what data type does method come under? Is there a way to add an indefinite data type?

23rd Sep 2020, 1:40 PM
Sourav Parik
5 Respuestas
+ 3
Let me review your question, you want to define a new methods that get the directory as a parameter. Is it true? Exmple 1; private void MyMethod(Dictionary<string,string> myDictionary) { //code } you can use object and then cast later like private void MyMethod(Object myDictionary) { string color = ((Dictionary<string,string>)myDictionary)["color"]; }
23rd Sep 2020, 3:06 PM
hossein B
hossein B - avatar
+ 3
Sourav Parik Like this?? Dictionary<int, Func<string, bool>> This allows you to store functions that take a string parameter and return boolean. dico[5] = foo => foo == "Bar"; https://stackoverflow.com/questions/4233536/c-sharp-store-functions-in-a-dictionary
23rd Sep 2020, 3:57 PM
hossein B
hossein B - avatar
+ 2
In this situation you must use delegate and then insert your input parameter in DynamicInvoke Like var dico = new Dictionary<int, Delegate>(); dico[1] = new Func<int, int, int>(Func1); var res = dico[1].DynamicInvoke(1, 2); Console.WriteLine(res); ... public int Func1(int arg1, int arg2) { return arg1 - arg2; } I wrote a program with subtraction https://code.sololearn.com/cC2mJ0XmosJ8/?ref=app
23rd Sep 2020, 5:25 PM
hossein B
hossein B - avatar
0
No, I want methods keyed to an int, and when another variable reaches that int, the method that is keyed to that int is added to an array This all is inside a class
23rd Sep 2020, 3:30 PM
Sourav Parik
0
hossein B But my function also subtractsa value, how do you define subtraction of values? is that an int too since it subtracts an int with another int?
23rd Sep 2020, 4:04 PM
Sourav Parik