如何字符串转换为整数,在C# [英] How to convert string to integer in C#

查看:172
本文介绍了如何字符串转换为整数,在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串转换为整数在C#?

How do I convert a string to an integer in C#?

推荐答案

如果你确定它会正确解析,用

If you're sure it'll parse correctly, use

int.Parse(string)

如果你不使用

int i;
bool success = int.TryParse(string, out i);

注意!在以下的情况下,将等于0,没有10后的的TryParse

Caution! In the case below, i will equal 0, not 10 after the TryParse.

int i = 10;
bool failure = int.TryParse("asdf", out i);

这是因为的TryParse 采用的的参数,而不是一个的 REF 的参数。

This is because TryParse uses an out parameter, not a ref parameter.

这篇关于如何字符串转换为整数,在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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