1

I am currently programming using Unity and C#, and I have trouble linking a string value to a function using a dictionary.

I think of a code looking like this :

private string name;

void function1()
    {
    // code
    }

private Dictionary<string, ?function?> nameToFunction = new Dictionary<string, ?function?>();
// The part between interrogation marks being unknown to me

// Trying to call the function with the name
nameToFunction[name]

I am sorry if my question isn't relative, or if there are simpler solutions I haven't thought of, but I am starting to learn programming.

Thanks for your answers !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Does this answer your question? [C# Store functions in a Dictionary](https://stackoverflow.com/questions/4233536/c-sharp-store-functions-in-a-dictionary) – Yong Shun May 21 '22 at 11:29
  • ...adding to Yong's fine link, in case the link's title is deceptive take note of the author's final point in the accepted answer of _"And if your functions doesn't have return values use `System.Action<>`"_ –  May 21 '22 at 12:44

3 Answers3

1

Here are some examples:

private Dictionary<string, Action> actionDict = new Dictionary<string, Action>();

private Dictionary<string, Action<int>> actionParamDict = new Dictionary<string, Action<int>>();

private Dictionary<string, Func<int>> funcDict = new Dictionary<string, Func<int>>();

private Dictionary<string, Func<int, string>> funcParamDict = new Dictionary<string, Func<int, string>>();
KiynL
  • 4,097
  • 2
  • 16
  • 34
  • 1
    Consider improving your answer. _[Code-only answers may fall under 'Very Low Quality' ...and are candidates for deletion....We've always touted that we aren't a code factory. We are the people who teach others to fish. Code-only answers only feed a person for a day](http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code-only-ques)_. Apart from that, you don't actually demonstrate _using_ the dictionary after creation. –  May 21 '22 at 12:45
0
//create dict with var objects s0, s1, s2 dynamically 
Dictionary<String, Object> dictionary = new Dictionary<String, Object>();
for(int i=0; i<sWPaths.Length-1; i++) {
string name = String.Format("s{0}", i);
dictionary[name] = i.ToString();
}

foreach (string p in found){
//change to your desired variable using string.format method
if(dictionary.Contains[p]) {
dictionary[p] = found.ToString();
 }
}

mailicreate
  • 136
  • 1
  • 4
0

You use Action e.g. Action<input type1, input type 2> if not returning a value, and Func<input type1, input type2, output type> to return a value. Both are defined with multiple numbers of inputs. So you could do Func<int,int,int,bool> for example, and Action<int,int,bool,string,string>

Here's a few examples:

var dict = new Dictionary<string, Action>();
dict.Add("Hello", () => Console.WriteLine("Hello"));
dict.Add("Goodbye", () => Console.WriteLine("Goodbye"));
dict["Hello"]();
dict["Goodbye"]();

var dict2 = new Dictionary<string, Action<string>>();
dict2.Add("HelloName", (name) => Console.WriteLine($"Hello {name}"));
dict2["HelloName"]("Fred");

var dict3 = new Dictionary<string, Func<int, int, int>>();
dict3.Add("ADD", (n1, n2) => n1 + n2);
dict3.Add("SUBTRACT", (n1, n2) => n1 - n2);
Console.WriteLine($"{dict3["ADD"](5, 10)}");
Console.WriteLine($"{dict3["SUBTRACT"](10, 5)}");
Stewart
  • 705
  • 3
  • 6