I need to check if the input is int or not. Is there a similar function for String.IsNullOrEmpty (), like the Int.isWholeNumber () function?
Is there a way to check this inside a statement if()without having the right to declare an int earlier? (How do you need to do with TryParse ())
if()
EDITI need to check the area code (five numbers)
I do not believe that there is such a method in BCL, but it is easy to write (I assume that you are really talking about whether a string can be parsed as an integer):
public static bool CanParse(string text) { int ignored; return int.TryParse(text, out ignored); }
, IFormatProvider .., . , , , ...
IFormatProvider
, Fluent Validation.
, - " , ?" - , . , . , , , , :
. , .
. , , "SE-12 345", "SE-" . , , .
. , . - , . , - , . , -, , ; , .
:
bool isNumber = "-1990".IsNumber();
public static class NumberStringExtension { public static bool IsNumber(this string value) { int i = 0; return int.TryParse(value, out i)); } }
Int.TryParse .
Int.TryParse
function bool IsNumber(object number) { try { Convert.ToInt32(number.ToString()); return true; } catch { return false; } }
, int.TryParse , , . ( , - "1." .)
string input; int ignored; bool wholeNumber = int.TryParse(input, out ignored) && input.indexOf('.') == -1;
Int types must be integers by definition. It must be a double, float, or decimal type for a non-integer. You can always try to check the input type.
Or maybe the input is a string? If the input is a string, you can try a search. or ',' (depending on the culture). Or you can try parsing as an integer.
Since each int is an integer, this is easy:
public static bool IsWholeNumber(int i) { return true; }
If you just want to check it in the if statement, you can just do
If ( val % 1 > 0) //not whole number