如何将无效值转换为0? [英] How do I convert invalid value to 0?

查看:71
本文介绍了如何将无效值转换为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:)我是C#的初学者。输入时我有数组填充数组。如果有人意外地提出无效值,例如单词或标点符号我需要将其转换为数字0。非常感谢!



我尝试过:



我尝试了布尔值if(输入是字符串)然后convert.toInt32

Hi :) I am beginner with C#. On input I have array filled with numbers. If someone accidentaly put invalid value e.g. word or punctuation I need to convert it to number 0 instead. Thank you very much!

What I have tried:

I have tried boolean value if (input is string) then convert.toInt32

推荐答案

使用 TryParse [ ^ ]方法,失败时分配 0 到变量(参见链接页面中的代码示例)。
Use the TryParse[^] method, on failure assign 0 to the variable (see the code sample in the linked page).


这篇关于如何将无效值转换为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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