Static ImmutableArray not initialized

I have one inner static class MyLists with a static member

internal static ImmutableArray<string> MyList = new ImmutableArray<string> { "asd", "qwe" };

In another test class for public tests, I have a MyTest function that selects and compares a list. I get

Additional information: The type initializer for 'TestMyLists' threw an exception. Object reference not set to an instance of an object.

[TestClass]
public class MyTests {
  [TestMethod]
  public void TestMyLists() {
    var list = MyLists.MyList.Select(s => s + ".foo");
  }
}

When I debug, I see the ImmutableArray static object as value = Uninitialized. Why?

+4
source share
1 answer

As mentioned in MSDN , you should use a method Create()instead of using a constructor for this array type:

internal static ImmutableArray<string> List = ImmutableArray.Create("asd", "qwe");

About the reason, I will point you to an article by Immo Landwerth, where he describes:

ImmutableArray<T> , . , ImmutableArray<T>, , .. Length 0, . , . , . ImmutableArray<T> IsDefault, true, null. , :

+5

Source: https://habr.com/ru/post/1660942/


All Articles