The essence of the program is as follows: a goal is selected, a command is entered into the textbox, which calls a method from a list of methods, which is different for each goal. How can I implement string matching and method in this case? Methods have the same signature and take an Object argument. For example, the entered command "zapros" displays true or flase (it all depends on the target you have chosen)

  • Only it would be better to add examples and details. That methods have the same signature and take object as the only parameter. - Vadim Ovchinnikov
  • 2
    switch ? - JVic
  • 2
    The methods that take Object as an argument are very bad methods, I would even say worthless methods. - Bulson
  • @Bulson, the point is that I'm trying to incorporate OOP into my projects. In general, an object enters the method from which the list and name are extracted: (SFile is a structure) SFile rf = (SFile) Obj; List <File_> files_ = rf.files_; string name_ = rf.name_; - ZOOM SMASH
  • @ZOOMSMASH You know the word "goal" is unclear here what it means. - Vadim Ovchinnikov

3 answers 3

An example of a console program. Using the interface, we organize different execution of the same method call. Call the desired method according to the user's choice, but it is executed differently depending on the class.

 //создаем интерфейс в котором описываем метод(ы) public interface IMakeSound { void Sound(int i = 1); void Noise(); } //cоздаем два класса реализующие этот интерфейс public class Car : IMakeSound { public void Noise() { Console.WriteLine("ИИИИИИИ!"); } public void Sound(int count = 1) { for (int i = 0; i < count; i++) { Console.WriteLine("Би-би!"); } } } public class Cow : IMakeSound { public void Noise() { Console.WriteLine("УУУУУУ!"); } public void Sound(int count = 1) { for (int i = 0; i < count; i++) { Console.WriteLine("Му-му!"); } } } class Program { static void Main(string[] args) { int i = 0; IMakeSound target = null; while (i == 0) { Console.WriteLine("Укажите цель (Корова, Машина, Выход):"); string targetString = Console.ReadLine(); switch (targetString) { case "Корова": target = new Cow(); i = 1; break; case "Машина": target = new Car(); i = 1; break; case "Выход": return; default: Console.WriteLine("Цель указана неверно, для выхода наберите Выход"); break; } } while (i == 1) { Console.WriteLine("Укажите задачу (Шум, Звук, Выход):"); string targetDoing = Console.ReadLine(); switch (targetDoing) { case "Шум": WantNoise(target); i = 2; break; case "Звук": WantSound(target); i = 2; break; case "Выход": return; default: Console.WriteLine("Задача указана неверно, для выхода наберите Выход"); break; } } Console.ReadKey(); } private static void WantNoise(IMakeSound obj) { obj.Noise(); } private static void WantSound(IMakeSound obj) { obj.Sound(2); } } 
  • I changed the example to GOAL-> Action - Bulson

For this program, you can implement the mapping using the dictionary:

 var methodTable = new Dictionary<string, Action<object>> { ["delete"] = delete, ["upload"] = upload, ["add"] = add }; 

where delete , upload , add program methods.

Then check the availability of this method using the ContainsKey method and, if available, call it by key.


If the names of the methods match the keys, then you can also use the nameof operator:

 var methodTable = new Dictionary<string, Action<object>> { [nameof(delete)] = delete, [nameof(upload)] = upload, [nameof(add)] = add }; 

You can also simply use if and switch to verify the key.

If you need a case-insensitive test, use if ("delete".Equals(textbox.Text, StringComparison.OrdinalIgnoreCase)) for if ("delete".Equals(textbox.Text, StringComparison.OrdinalIgnoreCase)) or switch text to lower or upper case using ToLowerInvariant or ToUpperInvariant respectively.


You can also use reflection, I iterate over the necessary classes, find the desired method. But this technique should be used with great care.

  • If each goal has its own set of methods? - ZOOM SMASH
  • @ZOOMSMASH That is a group that runs in turn? - Vadim Ovchinnikov
  • The command executes the method, depending on the goal that was selected at the beginning, that is, the method of a specific goal is called, each goal has its own set of methods - ZOOM SMASH
  • "the command performs the method" understood, and then did not understand anything. What does that mean before? - Vadim Ovchinnikov
  • one
    @ZOOMSMASH No, because I do not understand what the goal is more specifically. To help you, you need to describe it in a visual manner. What is a goal purely physically? Text field? PC username? - Vadim Ovchinnikov

You can use switch or if .

You analyze the text of the user and conditional statements cause the desired method.

If the methods have the same signature, then you can put these methods in the Dictionary<string,delegate> and pull the required method according to the key.

And then there is the pattern " team "

  • If each goal of the same methods are performed differently? - ZOOM SMASH