C#的测试,看看是否一个字符串是一个整数? [英] C# testing to see if a string is an integer?

查看:182
本文介绍了C#的测试,看看是否一个字符串是一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是古玩是否有一些内置或者C#语言或.NET Framework的测试,看看如果事情是一个整数

I'm just curios as to whether there is something built into either the c# language or the .net framework that tests to see if something is an integer

if (x is an int)
   // Do something

在我看来,有可能是,但我只是一个一年级编程的学生,所以我不知道。

It seems to me that there might be, but I am only a first-year programming student, so I don't know.

推荐答案

使用 int.TryParse 方法。

string x = "42";
int value;
if(int.TryParse(x, out value))
  // Do something

如果它成功地解析它会返回true,并且出的结果将其作为一个整数值。

If it successfully parses it will return true, and the out result will have its value as an integer.

这篇关于C#的测试,看看是否一个字符串是一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆