Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果方法参数为NULL,则返回值,否则不';不归_C#_.net - Fatal编程技术网

C# 如果方法参数为NULL,则返回值,否则不';不归

C# 如果方法参数为NULL,则返回值,否则不';不归,c#,.net,C#,.net,我知道如果对象为null,则有一些不同的方法可以使用它 不过我的问题有点不同。 也许我只是没有想清楚,可能有一个现有的解决方案 我想检查变量是否为空。 如果是,则从方法返回特定值。 如果不为null,则继续处理该方法 我需要在方法中的不同位置执行空检查。我的一些代码可能有三个或四个需要检查的输入参数。 因此,如果(parameter1==null | | parameter2==null),我就不能使用 考虑到下面的例子,最好的方法是什么? 我基本上是想减少所需的代码量 public List&

我知道如果对象为null,则有一些不同的方法可以使用它

不过我的问题有点不同。 也许我只是没有想清楚,可能有一个现有的解决方案

我想检查变量是否为空。
如果是,则从方法返回特定值。 如果不为null,则继续处理该方法

我需要在方法中的不同位置执行空检查。我的一些代码可能有三个或四个需要检查的输入参数。
因此,如果(parameter1==null | | parameter2==null),我就不能使用

考虑到下面的例子,最好的方法是什么? 我基本上是想减少所需的代码量

public List<OutputClass> GetOutputs(InputClass input1, OtherInputClass input2)
{
     if (input == null)
     {
          return new List<OutputClass>();
     }

     // continue with processing
     // do something with input1

     if (input2 == null)
     {
          return new List<OutputClass>();
     }

     // continue with processing
     // do something with input2

     var outputs = //some processing using the input data;

     return outputs;
}
公共列表GetOutputs(InputClass input1,OtherInputClass input2)
{
如果(输入==null)
{
返回新列表();
}
//继续处理
//用input1做点什么
if(input2==null)
{
返回新列表();
}
//继续处理
//用input2做点什么
var outputs=//使用输入数据进行一些处理;
返回输出;
}

在您描述的场景中,我通常会使用私有助手函数,尽管这最终取决于具体情况

例如:

public List<OutputClass> GetOutputs(InputClass inputOne, OtherInputClass inputTwo) {
     List<OutputClass> allTogether;

     // do something with inputOne
     allTogether = GetOutputsHelperOne(inputOne);

     // do something with inputTwo       
     allTogether = GetOutputsHelperTwo(inputTwo, allTogether);

     var outputs = // some processing using 'allTogether'

     return outputs; // or just return 'allTogether'
}

/*
 * Do something with inputOne
 */
private List<OutputClass> GetOutputsHelperOne(InputClass inputOne) {
    List<OutputClass> tmp = new List<OutputClass>();

    if (inputOne == null) {
          return tmp;
    }

    // else do something to 'tmp' with 'inputOne'
    return tmp;
}

/*
 * Do something with inputTwo
 */
private List<OutputClass> GetOutputsHelperTwo(OtherInputClass inputTwo, List<OutputClass> allTogether) {
    List<OutputClass> tmp = new List<OutputClass>();

    if (inputTwo == null) {
          return allTogether;
    }

    // else do something to 'tmp' with 'inputTwo'
    // or just manipulate 'allTogether' with 'inputTwo'

    // then merge 'tmp' with 'allTogether'
    // or just return 'allTogether'

    return allTogether;
}
公共列表GetOutputs(InputClass inputOne,OtherInputClass InputWO){
综合列出;
//用inputOne做点什么
合计=GetOutputsHelperOne(inputOne);
//做点什么
合计=GetOutputsHelperTwo(输入WO,合计);
var outputs=//使用“allTogether”进行某些处理
返回输出;//或者只返回'allTogether'
}
/*
*用inputOne做点什么
*/
私有列表GetOutputsHelperOne(InputClass inputOne){
List tmp=新列表();
如果(输入按钮==null){
返回tmp;
}
//还可以使用“inputOne”对“tmp”执行某些操作
返回tmp;
}
/*
*做点什么
*/
私有列表GetOutputsHelperTwo(其他InputClass InputWO,一起列出){
List tmp=新列表();
如果(输入WO==null){
一起返回;
}
//用“输入WO”对“tmp”执行其他操作
//或者只是用“inputWO”操纵“allTogether”
//然后将“tmp”与“allTogether”合并
//或者直接返回“一起”
一起返回;
}

很抱歉,如果上面的语法不正确,我不会在
C#
中编程,只是将其作为伪代码。

给定示例代码,如果其中任何一个参数为null,则返回一个新列表,我建议您提前完成所有参数验证,这样您就不会浪费任何处理时间,因为如果稍后的参数检查返回为null,这些操作将被丢弃:

public List<OutputClass> GetOutputs(InputClass input1, OtherInputClass input2)
{
    var outputs = new List<OutputClass>();

    if (input1 == null || input2 == null)
    {
        return outputs;
    }

    // continue with processing
    // do something with input1
    OutputClass output1 = GetOutput(input1.SomeValue);

    // do something with input2
    OutputClass output2 = GetOutput(new CustomClass(input2.AnotherValue));

    // create outputs based on the input data
    outputs.Add(output1);
    outputs.Add(output2);

    return outputs;
}
公共列表GetOutputs(InputClass input1,OtherInputClass input2)
{
变量输出=新列表();
if(input1==null | | input2==null)
{
返回输出;
}
//继续处理
//用input1做点什么
OutputClass output1=GetOutput(input1.SomeValue);
//用input2做点什么
OutputClass output2=GetOutput(新的CustomClass(input2.AnotherValue));
//根据输入数据创建输出
输出。添加(输出1);
输出。添加(输出2);
返回输出;
}
如果验证
input1
input2
之间所需的处理对程序状态进行了一些更改,则可能需要进行一些额外的重构。一般来说,方法应该尽可能谨慎,因为它们只做一件事。你必须小心副作用。从客户机的角度来看,如果其中一个或两个参数都为null,它们将获得相同的返回值。一般的期望是,在这3种情况下,项目的状态也将保持不变


然而,这是一项一般性建议;如果你举一个更具体的例子来说明你在做什么,可能会得到更好的答案。

这似乎是最好的方法。问题是什么?没有更好的方法了if语句的唯一“替代品”是开关,在本例中它不起作用。正确构造if语句,这是完全可能的。如果“某些[您的]代码可能有三个或四个需要[检查]的输入参数”,那么您的问题示例应该反映该场景,而不是您只有一个输入的场景。是的,听起来它需要一些重构
GetOutputs
应该只做一件事——根据提供的输入返回
OutputClass
的列表。如果它基于影响整个程序状态的一个或多个参数进行其他处理,那么它听起来像是一种代码气味,可能需要将额外的处理分解到不同的方法中。请注意,您的示例没有描述“我提到的处理工作方式有点不同”的含义,这使得很难对这个问题给出一个好的答案。