简体   繁体   English

static class 初始化 static 参考

[英]static class initialization with static references

In the following code, why Library.genres[n].booklist are null at runtime for all items in Library.genres ?在下面的代码中,为什么Library.genres[n].booklist在运行时对于Library.genres中的所有项目都是 null ? Just like if the definitions for dramabooks and kidsbooks were not being considered for the static initialization:就像dramabooks初始化没有考虑戏剧书和kidsbooks的定义一样:

static class Library
{
   public static List<Genre> genres = new List<Genre>()
   {
      new Genre("Drama", dramabooks),
      new Genre("Kids", kidsbooks)
   };

   public static List<Book> dramabooks = new List<Book>() {
      new Book("book1"), 
      new Book("book2")
   };

   public static List<Book> kidsbooks = new List<Book>(){
      new Book("book3"),
      new Book("book4")
   };
}

class Genre
{
   string catname;
   List<Book> booklist;

   Genre(string catname, List<Book> booklist)
   {
      this.catname = catname;
      this.booklist = booklist;
   }
}

class Book
{
   string title;

   Book(string title)
   {
      this.title = title;
   }
}

Disregarding any other code errors (and there are many), This is due to how static fields initialise, which is in the textual order of which they appear.忽略任何其他代码错误(还有很多),这是由于static 字段的初始化方式,这是它们出现的文本顺序。 kidsbooks and dramabooks haven't been initialised when genres initialises genres初始化时, kidsbooksdramabooks尚未初始化

From the ECMA C# Specifications来自ECMA C# 规格

15.5.6.2 Static field initialization 15.5.6.2 Static字段初始化

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration (§15.5.6.1). class 的 static 字段变量初始值设定项对应于按在 class 声明中出现的文本顺序执行的一系列赋值(§15.6)。 Within a partial class, the meaning of "textual order" is specified by §15.5.6.1.在部分 class 中,“文本顺序”的含义由 §15.5.6.1 指定。 If a static constructor (§15.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. If a static constructor (§15.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.否则,在第一次使用该 class 的 static 字段之前,在依赖于实现的时间执行 static 字段初始化程序。

In short, use a static constructor where you can express the order of initialisation explicitly, it's less prone to error and more declarative简而言之,使用 static 构造函数,您可以在其中显式表达初始化顺序,它不易出错且更具声明性

static class Library
{
   static Library()
   {

      dramabooks = new List<Book>()
      {
         new Book("book1"),
         new Book("book1")
      };

      kidsbooks = new List<Book>()
      {
         new Book("book3"),
         new Book("book4")
      };
      genres = new List<Genre>()
      {
         new Genre("Drama", dramabooks),
         new Genre("Kids", kidsbooks)
      };

   }

   public static List<Genre> genres;

   public static List<Book> dramabooks;

   public static List<Book> kidsbooks;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM