-7

如何在 c# 中检查函数参数是否为整数类型,例如,如果我有一个变量,我需要一种方法来检查该值是否为整数值。如果它是一个整数,则该方法返回 true,如果该值等于一个双精度值,则该方法返回 false。

4

2 回答 2

2

这就是您要查找的内容:

public static bool TryParse(
    string s,
    out int result
)

下面是一个实现示例:

string userInput = "4";
int convertedInput;
if(Int32.TryParse(userInput, out convertedInput) {
   //the userInput was a valid integer. convertedInput is now set to the integer equivalent of "4"
}
else {
   //the userInput was ***not*** a valid integer. 
}

这是 MSDN 文档:

Int32.TryParse 方法(字符串,Int32)

于 2013-04-25T22:13:48.380 回答
1

检查这些方法,它可能适合您的需求:

http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx

http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

于 2013-04-25T22:07:59.563 回答