Concat 2 lists when one is null

c#, concatenation, list

Solution

If at all possible, never let yourself be in the position where a list is `null`, so that you never need to check for `null` and handle that case any differently. Having an empty list instead of `null` is virtually always preferable.

Problem

When both lists below on the line of code are populated my code works fine. However the error "Value cannot be null." occurs when LstNewItems is set to null. Why and how can i fix this or do i have to check if each list is null beforehand and act accordingly ? ``` this.radGridViewFiles.BeginInvoke((MethodInvoker)( () => this.radGridViewFiles.DataSource = MyGlobals.ListOfItemsToControl .Concat(MyGlobals.lstNewItems) .ToList() )); ```

Original source