int.TryParse解析为对象数组 [英] int.TryParse out into an object array
本文介绍了int.TryParse解析为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组,我想检查字符串是否为数字,然后将它作为一个整数放入对象数组中。
I have a string array, and I want to check if a string is a number, and then put it in an object array as an int.
(对于那些问为什么要对象数组的人,因为我也想检查字符和其他东西)
(For those of you asking why an object array, because I want to check for characters and other things too)
我有这个:
Console.WriteLine("Enter parameters for the function with a space in between each parameter: "); String stringParameters = Console.ReadLine();
String[] parametersStringArray = stringParameters.Split(' ');
Object[] parametersArray = new Object[parametersStringArray.Length];
for (int i = 0; i < parametersStringArray.Length; i++)
{
int.TryParse(parametersStringArray[i], out int.Parse(parametersArray[i]));
}
它没有编译,我对'out'命令不熟悉,怎么了,怎么解决?
It doesn't compile and I am not familiar with the 'out' command, what's wrong and how do I fix it?
谢谢。
推荐答案
使用此代码
Console.WriteLine("Enter parameters for the function with a space in between each parameter: "); String stringParameters = Console.ReadLine();
String[] parametersStringArray = stringParameters.Split(' ');
Object[] parametersArray = new Object[parametersStringArray.Length];
for (int i = 0; i < parametersStringArray.Length; i++)
{
int tmp;
if (int.TryParse(parametersStringArray[i], out tmp))
parametersArray[i] = tmp;
}
这篇关于int.TryParse解析为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文