-2

This code is throwing and error 'cannot implicitly convert type void to object'

dynamic disruption1 = new ExpandoObject();
disruption1.locationsAffected = new string[] { "london", "panama" };
IEnumerable<dynamic> disruptionList = new List<dynamic>().Add(disruption1);

But I am not sure what is wrong, I also tried

dynamic disruptionList = new List<dynamic>().Add(disruption1);

and it doesn't throw the error but is not what I need.

I am creating a unit test.

Yatiac
  • 1,820
  • 3
  • 15
  • 25

1 Answers1

2

The return type of Add() method is void and you are trying to assign void to variable of type List<dynamic> which is absolutely not allowed and compiler will throw error which you are seeing,as you already have reference to the List<T>, add the item on the new line following way:

dynamic disruptionList = new List<dynamic>();
disruptionList.Add(disruption1);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160