Static Field Initializer Not Called in Windows Phone 8 C # Application

I have a static class with a static field that is initialized in place:

private static SomeType _instance = new SomeType(); 

This code is part of a portable class library that is used on multiple platforms. Everything works fine on desktop platforms, but when compiling for Windows Phone 8, _instance is null. But if I switch initialization to the default static constructor, initialization initializes correctly.

I tried to find an explanation for this behavior, but I did not find anything that could explain it.

UPDATE I spent some time creating a repeatable sequence of steps to reproduce the error, but at some point the error no longer occurred, even when I returned to the original code. I came to the conclusion that this is a false alarm, and the problem seems to be caused by something else. I do not feel comfortable leaving without explanation, but so far I have no reason to believe that this is due to the initialization of the static field.

+5
source share
1 answer

You need to make sure that nothing updates the static value, since static properties will be initialized when the application domain is created.

0
source

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


All Articles