C# 检查你是否传递了参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11791969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# check if you have passed arguments or not
提问by robertpas
I have this code:
我有这个代码:
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){...}
}
}
Also tried if(args.Length==0), but it still doesn't work.
也试过了if(args.Length==0),还是不行。
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.
我怎样才能做到这一点?提前致谢。
采纳答案by Albin Sunnanbo
if(args.Length==0)should work, args[0]requires at least one argument to not crash.
if(args.Length==0)应该可以工作,args[0]至少需要一个参数才能不崩溃。
回答by Tom
if(args == null || args.Length == 0)
{
// no arguments
}
else
{
// arguments
}
回答by Rune FS
it's an array and there's two scenarios that might have the meaning NO arguments passed. Depending on your semantics
它是一个数组,有两种情况可能意味着没有传递参数。取决于你的语义
args == nullor args.Length == 0
args == null或者 args.Length == 0
In this case where the method is called when the program is executed (e.g. not calling the method as part of say a unit test) the args argument will never be null (making the first test redundant) I've included it for completeness because the same situation might easily be encountered in other methods than main
在这种情况下,在程序执行时调用该方法(例如,不调用该方法作为单元测试的一部分),args 参数永远不会为空(使第一个测试变得多余)我将其包含在内是为了完整性,因为在 main 之外的其他方法中可能很容易遇到相同的情况
if you test them in that order you don't have to worry about args being null in the latter expression
如果您按该顺序测试它们,则不必担心 args 在后一个表达式中为 null
if(args == null || args.Length == 0){
ComputeNoParam cptern = new ComputeNoParam();
cptern.ComputeWithoutParameters();
}
else
{
ComputeParam cpter = new ComputeParam();
foreach (string s in args){...}
}
回答by Michelle
This should also work:
这也应该有效:
if (args.Length < 1)
{
//no args passed
}
回答by derpasaurus
Another available option if you're already using System.Linqis to make use of the Any()extension, for instance:
如果您已经在使用,另一个可用选项System.Linq是使用Any()扩展,例如:
public static void Main(string[] args)
{
if (args == null && !args.Any())
{
// No parameters passed.
ComputeNoParam cptern = new ComputeNoParam();
cptern.ComputeWithoutParameters();
return;
}
// process parameters
ComputeParam cpter = new ComputeParam();
foreach (string s in args){...}
}
This could also be written:
这也可以写成:
public static void Main(string[] args)
{
if (!args?.Any() ?? true)
{
// No parameters passed.
ComputeNoParam cptern = new ComputeNoParam();
cptern.ComputeWithoutParameters();
return;
}
// process parameters
ComputeParam cpter = new ComputeParam();
foreach (string s in args){...}
}
This just shows another option available to you, I'd agree with going with .Length, although I would drop the null check and use conditional access instead, so.
这只是向您展示了另一个可用的选项,我同意使用.Length,尽管我会放弃空检查并使用条件访问,所以。
if (args?.Length == 0) {
// Code hit if args is null or zero
}
回答by Janaka Dissanayake
This should work on your scenario:
这应该适用于您的场景:
if (args == null || args.Length == 0)
{
//Code when no arguments are supplied
}
else
{
//Code when arguments are supplied
}
Notice how check args == nullshould be executed before args.Length == 0when using || or &&. This is called "Condition Short-Circuiting" where C# will start evaluating the first condition and if it's true, will not look at the second condition. In this scenario, C# will evaluate the second condition only if the first condition is false.
注意在使用 ||check args == null之前应该如何执行 args.Length == 0或者 &&。这称为“条件短路”,其中 C# 将开始评估第一个条件,如果为真,则不会查看第二个条件。在这种情况下,仅当第一个条件为假时,C# 才会评估第二个条件。
Suppose if your conditions are aligned as if(args.Length == 0 || args == null)and argsbecome null,it will throw an exception on the first condition, although the second condition is true.
假设如果您的条件对齐为if(args.Length == 0 || args == null)并args变为null,它会在第一个条件下抛出异常,尽管第二个条件为真。
This is something we need to keep in mind when placing conditions.
这是我们在放置条件时需要牢记的。

