Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 条件语句摘要_C#_C# 4.0_Func - Fatal编程技术网

C# 条件语句摘要

C# 条件语句摘要,c#,c#-4.0,func,C#,C# 4.0,Func,如何总结以下条件语句 private static Func<T_Users, bool> GetFunc(short no = -1, decimal country = -1) { Func<T_Users, bool> exp; if (no == -1 && countryNo != -1) { exp = t => t.Country == country; } if (no !

如何总结以下条件语句

private static Func<T_Users, bool> GetFunc(short no = -1, decimal country = -1)
{
    Func<T_Users, bool> exp;

    if (no == -1 && countryNo != -1)
    { 
        exp = t => t.Country == country;
    }

    if (no != -1 && countryNo == -1)
    {
        exp = t => t.No == no;
    }

    if (no != -1 && countryNo != -1)
    {
        exp = t => t.No == no && t.Country == countryNo;
    }

    if (no == -1 && countryNo == -1)
    {
        exp = t => t.No != no && t.Country != countryNo;
    }
    return exp;
}
private static Func GetFunc(短号=-1,十进制国家=-1)
{
Func-exp;
如果(否==-1&&countryNo!=-1)
{ 
exp=t=>t.Country==Country;
}
如果(否!=-1&&countryNo==-1)
{
exp=t=>t.No==No;
}
如果(否!=-1&&countryNo!=-1)
{
exp=t=>t.No==No和&t.Country==countryNo;
}
如果(否==-1&&countryNo==-1)
{
exp=t=>t.No!=No&t.Country!=countryNo;
}
返回经验;
}
条件号高。事实并非如此。我认为有一个正确的方法,但我不知道。我不知道该怎么做。

C#团队花了一段时间才添加对默认参数的支持。他们可能已经考虑到了这个问题中的代码。您肯定不会从以下方面得到有关圈复杂度的警告:

private static Func<T_Users, bool> GetFunc() {
    return new Func<T_Users, bool>((t) => t.No == -1 && t.Country == -1);
}
private static Func<T_Users, bool> GetFunc(short no) {
    return new Func<T_Users, bool>((t) => t.No == no && t.Country == -1);
}
private static Func<T_Users, bool> GetFunc(decimal country) {
    return new Func<T_Users, bool>((t) => t.No == -1 && t.Country == country);
}
private static Func<T_Users, bool> GetFunc(short no, decimal country) {
    return new Func<T_Users, bool>((t) => t.No == no && t.Country == country);
}
private static Func GetFunc(){
返回新函数((t)=>t.No=-1和&t.Country=-1);
}
私有静态Func GetFunc(短编号){
返回新函数((t)=>t.No==No&t.Country==1);
}
私有静态Func GetFunc(十进制国家/地区){
返回新函数((t)=>t.No=-1&&t.Country==Country);
}
私有静态Func GetFunc(短编号,十进制国家/地区){
返回新函数((t)=>t.No==No&t.Country==Country);
}

您可以将
if
-语句移动到lambda函数中,如下所示:

Func<T_Users, bool> GetFunc(short no = -1, decimal country = -1)
{
    return t => (no == -1 || t.No == no)
             && (countryNo == -1 || t.Country == countryNo);
}
注意:这与您的代码并不完全相同,因为我怀疑您的代码有错误。如果-语句为,则您的最后一个

if (no == -1 && countryNo == -1)
{
    exp = t => t.No != no && t.Country != countryNo;
}
……但我想你的意思是:

if (no == -1 && countryNo == -1)
{
    // If 'no' and 'countryNo' are unspecified, anything is valid.
    exp = t => true;
}
我写上述代码时就考虑到了这一点

最后一句话:使用比
no
更多的描述性名称。我不能从你发布的代码中分辨出什么是
,过了一会儿你也分辨不出了

if (no == -1 && countryNo == -1)
{
    // If 'no' and 'countryNo' are unspecified, anything is valid.
    exp = t => true;
}