public bool CheckMobileSim(List<Mobile_Range> numberRange, string MobileNumber)
{
bool SimType = new bool();
string NineDigits = MobileNumber.Substring(0, 9).ToString();
long Number = Convert.ToInt64(NineDigits);
if (numberRange != null)
{
if (numberRange.Count > 0)
{
for (int i = 0; i < numberRange.Count; i++)
{
if ((Number >= numberRange[i].RangeStart && Number < numberRange[i].RangeEnd))
{
SimType = true;
break;
}
}
}
}
return SimType;
}
|
|
|||||
|
|
You can improve it like this:
Although
Why did I change the type of
Why did I threw exceptions in first two sanity checks and returned false in others?
Why did I change your variable names?
|
||||
|
|
|
Refactoring step 1, remove unnecessary variables and braces and return early
I realize that breaks with the "single point of return" wisdom but I'm not a big fan. When you have small functions like this one, it doesn't make all that much sense. Refactoring step 2, with that cleaned up it's easy to see how this fits into a simple LINQ query
Since this is a public method I added some checks and downcast List to IEnumerable, which is a looser contract and all you really need here. By the way, .Net naming conventions are pascalCase for private and local variables CamelCase for public and protected. It's rare to use underscores. |
|||||||||||||||||
|
This way you don't need a loop but there is a loop behind. |
|||||||
|