Array's element number

If there is say array like this:

Element 0 : Dog
Element 1 : Cat
Element 2 : Cow
Element 3 : Horse

Is there a way to find out the element number if I know the content?

So I would just give the code word “Cat” and it would return the element number “1”.

Now I’m just making lots of if statements to find this out like:

if (lastName == "Dog")
number = 0;

if (lastName == "Cat")
number = 1;

I was wondering if there was better way to do this?

Dictionary is exactly the data structure you want: C# Dictionary Examples - Dot Net Perls

You need to add “using System.Collections.Generic;” to your file.

Dictionary<string,int> myDictionary = new Dictionary<string,int>();
myDictionary.Add("Dog",1);

//Then you can access like:
Debug.Log("Value of Dog " + myDictionary["Dog"]);

//Output is: 1

Alternately:

string[] pets = new string[2] { "Dog", "Cat" };
int index = System.Array.IndexOf(pets, "Dog");