I stumbled at the very beginning, I can not initialize the list, apparently due to the fact that the list is unchangeable. How to work with it? How to assign an array to it?

ImmutableList<int> firstList = new ImmutableList<int>(); 

enter image description here

  • one
  • @HasmikGaryaka I read about MSDN for a long time, unfortunately I didn’t figure it out, there is no example code for this class - StriBog
  • @HasmikGaryaka to use class methods for this variable, it needs to be initialized (allocate memory for it), which is what I have shown in the screenshot - StriBog
  • If you take an array that supports IEnumerable, you can also through AddRange - HasmikGaryaka

3 answers 3

Here is an example of creating

 var builder = ImmutableList.CreateBuilder<string>(); builder.Add("1"); // Adds item to the existing object ImmutableList<string> list = builder.ToImmutable(); ImmutableList<string> list2 = list.Add("2"); // Creates a new object with 2 items 

Taken from here

    Another option:

     int[] data = {1, 2, 3}; ImmutableList<int> firstList = ImmutableList.CreateRange(data); 

      or so:

       int[] data = {1, 2, 3}; var firstList = data.ToImmutableList();