Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm just curious 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
257 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use the int.TryParse method.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...