C#检查您是否传递了参数 [英] C# check if you have passed arguments or not
本文介绍了C#检查您是否传递了参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
public static void Main(string[] args)
{
if (string.IsNullOrEmpty(args[0])) // Warning : Index was out of the bounds of the array
{
ComputeNoParam cptern = new ComputeNoParam();
cptern.ComputeWithoutParameters();
}
else
{
ComputeParam cpter = new ComputeParam();
foreach (string s in args){...}
}
}
还尝试了 if(args.Length == 0)
,但仍然无法正常工作.
Also tried if(args.Length==0)
, but it still doesn't work.
基本上,我想确定用户是否使用参数调用了该程序.如果没有,程序将要求输入.
Basically I want to find out if the user called the program with arguments. If not the program will ask for input.
我该怎么做?预先感谢.
How can I do this? Thanks in advance.
推荐答案
if(args.Length == 0)
应该起作用, args [0]
至少需要一个不崩溃的论点.
if(args.Length==0)
should work, args[0]
requires at least one argument to not crash.
这篇关于C#检查您是否传递了参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文