Giving parameters to a Func.

Hey, I am trying to make a command line that gets a string and the I call a function based off of said string. I am trying to use dictionaries because I think that'll be better than a super long list of if statements. But, I have run into an issue where I cannot figure out how to give a Funt parameters. Here's the code:
namespace example
{
public static class CommandLine
{
// Dictionary with all valid commands and then the corresponding method.
private static Dictionary<string, Func<string>> validCommands = new Dictionary<string, Func<string>>()
{
{"test", TestMethod}
};

// Called from else where in the program with the raw command string. Ex: "/test"
public static string CommandParser(string rawCommand)
{
if (rawCommand != "")
{
// Removes the '/' from the string and split it to an array of words.
string[] formattedCommand = rawCommand.Remove(0, 1).Split(" ");
try
{
// Finds the command and runs the appropiate function.
return $"Output: {validCommands[formattedCommand[0]].Invoke()}";
}
catch (KeyNotFoundException)
{
// If command isn't found this will be printed.
return $"Invalid command \"{rawCommand}\".";
}
}
else
{
// Returns an empty string to allow for new lines.
return "";
}
}

public static string TestMethod()
{
return "Yay it worked";
}
}
}
namespace example
{
public static class CommandLine
{
// Dictionary with all valid commands and then the corresponding method.
private static Dictionary<string, Func<string>> validCommands = new Dictionary<string, Func<string>>()
{
{"test", TestMethod}
};

// Called from else where in the program with the raw command string. Ex: "/test"
public static string CommandParser(string rawCommand)
{
if (rawCommand != "")
{
// Removes the '/' from the string and split it to an array of words.
string[] formattedCommand = rawCommand.Remove(0, 1).Split(" ");
try
{
// Finds the command and runs the appropiate function.
return $"Output: {validCommands[formattedCommand[0]].Invoke()}";
}
catch (KeyNotFoundException)
{
// If command isn't found this will be printed.
return $"Invalid command \"{rawCommand}\".";
}
}
else
{
// Returns an empty string to allow for new lines.
return "";
}
}

public static string TestMethod()
{
return "Yay it worked";
}
}
}
My question is, how do I modify this code so I can add parameters to TestMethod and still call it in this way. Or even if there is a better/cleaner way to build something like this I'll look into what ever information I'm provided. Thank you so much for any and all assistance.
2 Replies
Angius
Angiusβ€’5mo ago
Seeing how it's a Func<string>, it's going to be an equivalent of string Func() So no parameters If you want to add params to it, sure, go ahead And you can just... call it
if (dict.TryGetValue(key, out var func))
{
result = func(one, two, three);
}
if (dict.TryGetValue(key, out var func))
{
result = func(one, two, three);
}
MODiX
MODiXβ€’5mo ago
Angius
REPL Result: Success
var funcs = new Dictionary<string, Func<int, int, string>>(){
["sum"] = (int a, int b) => $"a plus b is {a + b}",
["sub"] = (int a, int b) => $"a minus b is {a - b}"
};

var param = "sub";

if (funcs.TryGetValue(param, out var func))
{
Console.WriteLine(func(420, 69));
}
var funcs = new Dictionary<string, Func<int, int, string>>(){
["sum"] = (int a, int b) => $"a plus b is {a + b}",
["sub"] = (int a, int b) => $"a minus b is {a - b}"
};

var param = "sub";

if (funcs.TryGetValue(param, out var func))
{
Console.WriteLine(func(420, 69));
}
Console Output
a minus b is 351
a minus b is 351
Compile: 514.552ms | Execution: 86.921ms | React with ❌ to remove this embed.