C# testing to see if a string is an integer?

后端 未结 10 1443
北恋
北恋 2020-11-28 12:00

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         


        
10条回答
  •  猫巷女王i
    2020-11-28 12:12

    Use the int.TryParse method.

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

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

提交回复
热议问题