Suppose I have a sorted array of integers int[]
, and I want to search the closest smaller value to some input number.
for example if the array contains (1)
I'd go for a linq solution (updated: to add a little more code to stop from being similar to fear's similar solution):
int[] arr1 = { 1, 23, 57, 59, 120 };
int maxResult;
string errorMsg;
try
{
maxResult = arr1.Where(x => x <= 109).Max();
}
catch(Exception e)
{
errorMsg = e.Message;
// do some error stuff here :)
return null;
}
// party on your maxResult...