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()
));
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.
What I do is create an extension method that can create an empty list if the enumeration is null:
public static IEnumerable<T> NeverNull<T>(this IEnumerable<T> value)
{
return value ?? Enumerable.Empty<T>();
}
Then you can do:
this.radGridViewFiles.BeginInvoke((MethodInvoker)(
() => this.radGridViewFiles.DataSource = MyGlobals.ListOfItemsToControl
.Concat(MyGlobals.lstNewItems.NeverNull())
.ToList()
));
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With