I am trying to print out the exact location of an array element but am coming out short
string[] ocean = { "Beebo", "Jeff","Arthur", "Nemo", "Dory" };
foreach(string fish in ocean)
{
if (fish == "Nemo")
{
Console.WriteLine("We found Nemo on position {0}!",int.Parse(fish));
return;
}
}
Console.WriteLine("He was not here");
I need the {0} token to be replaced with the array index of that element in this case with 3 but i am failing at the int.Parse(fish) which obviously isn't working
Array.IndexOfto get the index of the fish name. Otherwise either use aforloop where you control the index or add a variable that you increment in theforeachto keep track of the index.string searchString = "Nemo"; int[] position = Enumerable.Range(0, ocean.Length).Where(idx => ocean[idx].Equals(searchString)).ToArray();