It's a trick question.
public string FindStringEquivalent(long numberToFind)
You wouldn't be using the return type of string if you're dealing with returning a number for a start.
So you wouldn't need to call to string() if the type was right in the first place.
If your enumerable range is between 1 , 10, then you wouldn't need to use long
FindString(long findnumber)
Everything is wrong with the question, and the code, and its a bad example to test someone with because It kinda doesn't make sense.
Anyway... if you know your element range is between 1, 10, that means that the indexes of the Enumerable type will start from 0 and that means 1 will be at the index of 0. So you could simply minus 1 from the number you want to find, like so :
public static string FindString(short findnumber)
{
return Enumerable.Range(1, 10).ElementAt(findnumber - 1).ToString();
}
This code took 2ms to execute. While the following code took 3.7ms to execute :
public static string FindString(short findnumber)
{
return ((short)Enumerable.Range(1, 10).Take(findnumber).ElementAt(findnumber - 1)).ToString();
}
This variation takes the number to find from the elements position minus one of the number to find.
While this variation loops with an enumerable step, which moved through the Enumerable Range of numbers, and checks if the number in the range was found. And this only took 1ms :
public static string FindString(short findnumber)
{
using (IEnumerator<int> number = Enumerable.Range(1, 10).GetEnumerator())
{
while (number.MoveNext())
if (number.Current.Equals(findnumber))
return number.Current.ToString();
}
return null;
}
This variation is the same as the one above except its using a traditional foreach iteration of the Enumerable Range. This also took exactly 1ms :
public static string FindString(short findnumber)
{
foreach (int numeber in Enumerable.Range(1, 10))
if (numeber.Equals(findnumber))
return numeber.ToString();
return null;
}
In all examples, this is all pointless unless you wanted to know the index of the position of the number in the Enumerable Range. But since that's not the question at hand, this should answer your question. Does it not?
But if you did want the index of where the number can be found in the Enumerable Range instead, you can do this which executes at 1ms also :
public static string FindString(short findnumber)
{
int index = 0;
foreach (int number in Enumerable.Range(1, 10))
{
if (number.Equals(findnumber))
return index.ToString();
index++;
}
return null;
}
If you wanted both, the number of the index in the Enumerable Range and the number itself, if it can be found, you need to change your return type to something like this :
public static int[] FindString(int findnumber)
{
int index = 0;
foreach (int number in Enumerable.Range(1, 10))
{
if (number.Equals(findnumber))
return new int[] { index, number };
index++;
}
return null;
}
Note, your calling code would be :
int[] x = FindString(8);
and x[0] will contain the index and x[1] will contain your number. Your result looking like int[2] => {[0] 7, [1] 8}
But its a pointless test, and redundantly silly.