I want to be able to simplify my code for the future. So I was thinking that I could try using the name/value of a variable to figure out which method should be used, but I don't know the syntax for this if there is any. I wish to do this inside the type (i.e. as a part of the static class instead of an instance of the class, as answers to similar questions are only for instances of the class).
For example, I had the user input a string, and detect if the first word was found in a predefined list, like "help", "get", or "time". The first solution I thought of was having a big chain of conditionals to see if it matches.
public static void parsing(string userInput)
{
//code for finding the value left out for brevity
string firstWord = /*whatever is needed*/;
switch(firstword)
{
//Command is the parent class
case "help":
Command.help(words[1..^1]);
break;
case "get":
Command.get(words[1..^1]);
break;
//and so on
}
}
However, I realized that this would get very tedious to maintain and to add or remove new instances from. I was wondering if there was a syntax in C# that would allow this. I know of an example in JS where this thing can be accomplished, it looks like the following:
class exampleClass {
static getData() {
return "someData"
}
static getData1() {
return "otherData"
}
static allData(data) {
return exampleClass[data]()
}
}
Where exampleClass[data]()
will be able to call that method inside of the class and allows for easier use. I would like to do something like this in my program, but I don't know the analog for it in C#. What should I do?