public static void Main()
var phoneType = "WorkPhone";
var phoneTypePref = "HomePhone";
int result = getPhoneIndex(phoneTypePref, homePhone, cellPhone, workPhone, phoneType);
Console.WriteLine(result);
static int getPhoneIndex(string phoneTypePref, string homePhone, string cellPhone, string workPhone, string phoneType)
string[] phoneNums = {homePhone,cellPhone,workPhone};
string[] phoneTypes = {"HomePhone","CellPhone","WorkPhone"};
int[] phoneIndices = {0,1,2};
int maxIndex = phoneNums.Length - 1;
for (int i = 0; i<3 ; i++)
if (String.IsNullOrEmpty(phoneNums[i]))
phoneIndices[i] = maxIndex;
else if (phoneTypes[i] == phoneTypePref){
phoneIndices[i] = minIndex;
for (int j = 0; j<3 ; j++)
if ((phoneTypes[j] != phoneTypePref) && !String.IsNullOrEmpty(phoneNums[j])){
phoneIndices[j] = minIndex;
return phoneIndices[Array.IndexOf(phoneTypes,phoneType)];
static int getPhoneIndex(string phoneTypePref, string homePhone, string cellPhone, string workPhone, string phoneType)
var phone = new Dictionary<string, string>()
{ "HomePhone", homePhone },
{ "CellPhone", cellPhone },
{ "WorkPhone", workPhone }
var phoneList = new List<KeyValuePair<string, string>>();
var emptyNumbers = new List<KeyValuePair<string, string>>();
foreach (KeyValuePair<string, string> pair in phone)
if (string.IsNullOrEmpty(pair.Value))
else if (pair.Key == phoneTypePref)
phoneList.Insert(0, pair);
if (emptyNumbers.Count > 0)
phoneList.AddRange(emptyNumbers);
return phoneList.IndexOf(phoneList.Where(a => a.Key == phoneType).FirstOrDefault());