如何检测var是否为字符串

fac*_*orx 3 c# string variables if-statement detect

我在使用if语句来检测我的numericupdown对象是否是一个字符串时遇到了一些麻烦,因此我不使用十进制变量来调整它.我看到有一种类型的命令,但我在使用它时遇到了麻烦.以下是我要看的内容:

if(typeof sentNUD.Value == string)
{
       //Do string arguments here
}
else
{
        //do decimal arguments here
}
Run Code Online (Sandbox Code Playgroud)

if语句的语法虽然错误但我收到错误.如何正确设置if语句?我尝试了一下,但我无法理解如何正确地做到这一点.谢谢.

nvo*_*igt 17

你可以使用"是" - 运营商:

if(variable is string)
{

}
Run Code Online (Sandbox Code Playgroud)


Cam*_*uce 7

if (sentNUD.Value.GetType() == typeof(string))
{
// string stuff
}
Run Code Online (Sandbox Code Playgroud)