Home > Software design >  Value cannot be null paramater name second
Value cannot be null paramater name second

Time:02-28

I have the following code. Here the newList is null because of which I am getting an error. How can I prevent this.

combinedList = (combinedList ?? new List<combinedList>()).Concat(newList).ToList();

Which is returning me the following error.

'Value cannot be null. Parameter name: second'

CodePudding user response:

You could just use ?? again:

combinedList = (combinedList ?? new List<combinedList>())
    .Concat(newList ?? new List<combinedList>())
    .ToList();

Though it seems like you really just want this:

combinedList ??= new List<combinedList>();
if (newList != null)
{
    combinedList.AddRange(newList);
}
  •  Tags:  
  • c#
  • Related