0

I have the following piece of code:

private List<IComponent> _components;

protected void UsePageComponents(params IComponent[] components) => _components = components.ToList();

I receive an array with params and I would like to append this array to the List (_components)`? How do I accomplish this using LINQ?

Drago
  • 1,755
  • 3
  • 19
  • 30
  • 4
    `_components.AddRange(components);`? – Crypt32 Mar 18 '20 at 16:29
  • [List.AddRange](https://learn.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/z883w3dc(v%3dvs.95)) – styx Mar 18 '20 at 16:30
  • 1
    Does this answer your question? [Adding string array (string\[\]) to List c#](https://stackoverflow.com/questions/12882778/adding-string-array-string-to-liststring-c-sharp) The answer is valid fro any type of objects – Pavel Anikhouski Mar 18 '20 at 16:32
  • Does this answer your question? [Conversion of System.Array to List](https://stackoverflow.com/questions/1603170/conversion-of-system-array-to-list) – Wariored Mar 18 '20 at 16:34

1 Answers1

1

You don't need LINQ; List<T>.AddRange will do:

protected void UsePageComponents(params IComponent[] components)
{
    _components.AddRange(components);
}

It accepts IEnumerable<T> which T[] implements.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35