Order of static constructors/initializers in C#

后端 未结 4 1439
梦如初夏
梦如初夏 2020-12-03 10:02

While working on a C# app I just noticed that in several places static initializers have dependencies on each other like this:

static private List         


        
4条回答
  •  孤城傲影
    2020-12-03 10:45

    Personally I would get rid of the static initializers since it isn't clear and add a static constructor to initialize these variables.

    static private List a;
    static private List b;
    
    static SomeClass()
    {
        a = new List() { 0 };
        b = new List() { a[0] };
    }
    

    Then you don't have to guess what is going on and you're being clear in your intentions.

提交回复
热议问题