简体   繁体   English

C#检查您是否已通过参数

[英]C# check if you have passed arguments or not

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. 提前致谢。

if(args.Length==0)应该有效, args[0]至少需要一个参数才能崩溃。

if(args == null || args.Length == 0)
{
    // no arguments
}
else
{
    // arguments
}

it's an array and there's two scenarios that might have the meaning NO arguments passed. 它是一个数组,并且有两个场景可能具有传递NO参数的含义。 Depending on your semantics 取决于您的语义

args == null or args.Length == 0 args == nullargs.Length == 0

In this case where the method is called when the program is executed (eg 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){...}
}

This should also work: 这应该也有效:

if (args.Length < 1)
{
    //no args passed
}

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 == null should be executed before args.Length == 0 when using || 注意在使用||时,如何在args.Length == 0之前执行check args == null 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. 这被称为“条件短路”,其中C#将开始评估第一个条件,如果它是真的,则不会查看第二个条件。 In this scenario, C# will evaluate the second condition only if the first condition is false. 在这种情况下,只有在第一个条件为假时,C#才会评估第二个条件。

Suppose if your conditions are aligned as if(args.Length == 0 || args == null) and args become 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. 在放置条件时我们需要记住这一点。

Another available option if you're already using System.Linq is 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM