C# 表达式始终为真,再竖琴提示

C# 表达式始终为真,再竖琴提示,c#,optimization,resharper,C#,Optimization,Resharper,编辑:谢谢您的回答,那么应该如何写才能在每一行中清楚说明条件是什么,还是为了可读性而保持原样 我采用了from来返回空数组,而不是抛出错误 public static T[] Concat<T>(this T[] x, T[] y) { if (x == null && y != null) return y; if (x != null && y == null) return x; if (x == null

编辑:谢谢您的回答,那么应该如何写才能在每一行中清楚说明条件是什么,还是为了可读性而保持原样

我采用了from来返回空数组,而不是抛出错误

public static T[] Concat<T>(this T[] x, T[] y)
{
    
    if (x == null && y != null) return  y;
    if (x != null && y == null) return  x;
    if (x == null && y == null) return new T[0];

    int oldLen = x.Length;
    Array.Resize<T>(ref x, x.Length + y.Length);
    Array.Copy(y, 0, x, oldLen, y.Length);
    return x;
}
公共静态T[]Concat(此T[]x,T[]y)
{
如果(x==null&&y!=null)返回y;
如果(x!=null&&y==null)返回x;
如果(x==null&&y==null)返回新的T[0];
int oldLen=x.长度;
调整数组大小(参考x,x.长度+y.长度);
数组.Copy(y,0,x,oldLen,y.Length);
返回x;
}
Resharper正在画一条曲线,“表达式始终为真”提示:

我不明白的是,对于2个不同的变量,应该有4个不同的情况,每个变量2个,4的排列:(x为null,x为notnull)(y为null,y为notnull)

因此,总共有4个案例,我试图在if中捕获

更改线的顺序只会将扭曲线移动到最后一条if线

if (x == null && y != null) return  y;
if (x != null && y == null) return  x;
if (x == null && y == null) return new T[0];
1如果x为null且y不为null,则返回y

2如果x不为null且y为null,则返回x

3如果x为null,那么根据第一个条件y必须为null

1如果x为null且y不为null,则返回y

2如果x不为null且y为null,则返回x

3如果x为空,那么根据第一个条件y必须为空。

布尔代数和命题逻辑骗局

当你写这个的时候

if (x == null && y != null) return  y;
然后,当您再次比较
x==null
,那么
y==null
天生就是真的

不可能是别的

更新

if (x == null) return y ?? new T[0];
if (y == null) return x;
...

int oldLen = x.Length;
Array.Resize<T>(ref x, x.Length + y.Length);
Array.Copy(y, 0, x, oldLen, y.Length);
return x;
如果(x==null)返回y??新T[0];
如果(y==null)返回x;
...
int oldLen=x.长度;
调整数组大小(参考x,x.长度+y.长度);
数组.Copy(y,0,x,oldLen,y.Length);
返回x;
就像你将得到的一样简单

布尔代数和命题逻辑把戏

当你写这个的时候

if (x == null && y != null) return  y;
然后,当您再次比较
x==null
,那么
y==null
天生就是真的

不可能是别的

更新

if (x == null) return y ?? new T[0];
if (y == null) return x;
...

int oldLen = x.Length;
Array.Resize<T>(ref x, x.Length + y.Length);
Array.Copy(y, 0, x, oldLen, y.Length);
return x;
如果(x==null)返回y??新T[0];
如果(y==null)返回x;
...
int oldLen=x.长度;
调整数组大小(参考x,x.长度+y.长度);
数组.Copy(y,0,x,oldLen,y.Length);
返回x;

如果我们查看您的第一张支票和最后一张支票,您将得到最简单的支票:

if (x == null && y != null) return  y;
if (x == null && y == null) return new T[0];
注意,我们已经测试了
x==null&&y!=null
,所以如果我们通过了检查,那么我们肯定知道如果
x==null
y
已经为null(如果不是,那么我们已经返回了
y

这里有一种方法可以检查相同的条件,而无需重复检查:

if (x == null && y == null) return new T[0];   // If they're both null, return a new thing
if (x == null) return y;                       // Otherwise if only one of them is null,
if (y == null) return x;                       // then return the other one
或者,如果你喜欢的话,你可以在一行中完成这一切:

if (x == null || y == null) return x == null ? y == null ? new T[0] : y : x;

如果我们查看您的第一张和最后一张支票:

if (x == null && y != null) return  y;
if (x == null && y == null) return new T[0];
请注意,我们已经测试了
x==null&&y!=null
,因此如果我们通过了该检查,那么我们肯定知道如果
x==null
y
已经为null(如果不是,那么我们已经返回了
y

这里有一种方法可以检查相同的条件,而无需重复检查:

if (x == null && y == null) return new T[0];   // If they're both null, return a new thing
if (x == null) return y;                       // Otherwise if only one of them is null,
if (y == null) return x;                       // then return the other one
或者,如果你喜欢的话,你可以在一行中完成这一切:

if (x == null || y == null) return x == null ? y == null ? new T[0] : y : x;

您的第一个条件符合
y==null
。您的编辑现在更像是一个基于意见的问题。我个人更喜欢@MichaelRandall的答案中的两行版本,然后是我的答案中的三行版本。但是如果您更喜欢您编写的版本,那么就使用它!最终不会有任何区别。考虑了什么“可读性”在一定程度上取决于您阅读代码的时间。我仍然无法始终在第一次阅读Linq
Zip
语句。:)@鲁弗斯:我刚刚明白我需要什么,迈克尔·兰德尔的代码就是我想要的,谢谢!你的第一个条件符合
y==null
。你的编辑现在更像是一个基于意见的问题。我个人更喜欢@MichaelRandall的答案中的两行版本,然后是我答案中的三行版本。但是如果你喜欢的话,你可以死记硬背,然后使用它!它最终不会有任何区别。所谓“可读”部分取决于您阅读代码的时间。我仍然不能总是第一次阅读Linq
Zip
语句。:)@我刚刚明白我需要什么,Michael Randall的代码就是我想要的,谢谢!一个问题,如果它们都为null呢?我需要发送一个新的空数组。一个问题,如果它们都为null呢?我需要发送一个新的空数组。