Search code examples
c#chaining

C# Function Chaining


Why do i receive error in the following declaration ?

List<int> intrs = new List<int>().AddRange(new int[]{1,2,3,45});

Error :Can not convert type void to List ?


Solution

  • Because AddRange function does not return a value. You might need to perform this in two steps:

    List<int> intrs = new List<int>();
    intrs.AddRange(new int[]{1,2,3,45});