1. Напишите функцию, которая принимает массив ['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'] и возвращает индекс элемента «needle». решил ее вот так, но учитель ждет другого решения, есть варианты? static void Main(string[] args) { string[] main = new string[7] { "hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk" }; question(main); Console.ReadLine(); } static void question(string[] main) { for (int i = 0; i < main.Length; i++) { string needle = "needle"; if (main == "needle") { Console.WriteLine(needle); } else { continue; }
static void Main(string[] args) { string[] main = new string[7] { "hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk" }; string needle = "needle"; var index = IndexOf(needle, main); if (index == -1) { Console.WriteLine("Не найден"); } else { Console.WriteLine($"Индекс: {index}"); } Console.ReadLine(); } static int IndexOf(string value, string[] values) { for (int i = 0; i < values.Length; i++) { if (values[i] == value) { return i; } } return -1; } C# static void Main(string[] args) { string[] main = new string[7] { "hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk" }; string needle = "needle"; var index = IndexOf(needle, main); if (index == -1) { Console.WriteLine("Не найден"); } else { Console.WriteLine($"Индекс: {index}"); } Console.ReadLine(); } static int IndexOf(string value, string[] values) { for (int i = 0; i < values.Length; i++) { if (values[i] == value) { return i; } } return -1; }