I suggest organizing all strings and corresponding methods as a Dictionary
:
Dictionary<string, Action> myCars = new Dictionary<string, Action>() {
{"FORD", method1}, // e.g. {"FORD", () => {Console.WriteLine("It's Ford!");}},
{ "KIA", method2},
{ "BMW", method3},
//TODO: Put all the cars here
};
then we can put a simple loop:
foreach (var pair in myCars)
if (file.Contains(pair.Key)) { // if file contains pair.Key
pair.Value(); // we execute corresponding method pair.Value
break;
}
Edit: In case we can have complex methods (e.g. method may want file
and key
parameters) we can change signature:
// Each action can have 2 parameters: key (e.g. "FORD") and file
Dictionary<string, Action<string, string>> myCars =
new Dictionary<string, Action<string, string>>() {
{"FORD", (key, file) => {Console.Write($"{key} : {string.Concat(file.Take(100))}")}},
{ "KIA", (key, file) => {Console.Write($"It's {key}!")}},
{ "BMW", (key, file) => {/* Do nothing */}},
//TODO: Put all the cars here
};
When executing in the loop, we should provide these parameters:
foreach (var pair in myCars)
if (file.Contains(pair.Key)) { // if file contains pair.Key
pair.Value(pair.Key, file); // we execute corresponding method pair.Value
break;
}