0

EDIT: Thank you for the answers, so how should it be written to be clear in each line what the condition is, or just leave it as it is for the sake of readability?

I adopted an answer from How do I concatenate two arrays in C#? to return an empty array instead of throwing errors

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;
}

Resharper is making the squiggly line, "expression is always true" hint: enter image description here

What I don't understand is that there should be 4 distinct cases for 2 distinct variables, 2 for each, permutation of 4 : (x is null,x is not null)(y is null,y is not null)

so all together there are 4 cases that I have tried to capture in the if's

changing the order of the lines just moves the squiggly line to the last if line.

Community
  • 1
  • 1
jimjim
  • 2,414
  • 2
  • 26
  • 46
  • 1
    your first condition qualifies `y == null`. – Daniel A. White Feb 14 '18 at 01:38
  • Your edit is now more of an opinion-based question. I personally prefer the two line version in the answer by @MichaelRandall, followed by the three line version in my answer. But if you prefer the one you wrote, then use that! It won't make any difference in the end. What is considered "readable" depends in part on how long you've been reading code. I still can't always read a Linq `Zip` statement the first time through. :) – Rufus L Feb 14 '18 at 02:08
  • @RufusL : I just understood what I need, Michael Randall's code is all I wanted, thanks! – jimjim Feb 14 '18 at 02:28

3 Answers3

2

If we look at the first and last of your checks:

if (x == null && y != null) return  y;
if (x == null && y == null) return new T[0];

Notice that we've already tested if both x == null && y != null, so if we made it past that check, then we know for certain that if x == null y has to be null (if it wasn't then we would have already returned y.

Here's a way to check the same conditions without the redundant check:

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

Or you can do it all in one line if that's your thing:

if (x == null || y == null) return x == null ? y == null ? new T[0] : y : x;
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1
if (x == null && y != null) return  y;
if (x != null && y == null) return  x;
if (x == null && y == null) return new T[0];

1 if x is null and y is not null you return y

2 if x is not null and y is null you return x

3 if x is null, then y has to be null based on your first condition.

tonedblue
  • 164
  • 1
  • 6
1

The Boolean Algebra and Proposition Logic shenanigans

When you write this

if (x == null && y != null) return  y;

Then when you again compare x == null, then y == null is true innately

It cant be anything else

Update

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;

About as simple as you will get

TheGeneral
  • 79,002
  • 9
  • 103
  • 141