Initialization of instance fields vs. local variables

前端 未结 7 520
余生分开走
余生分开走 2020-11-27 06:07

I have always been wondering about why in the following example it is OK to not initialize the instance field (relying that it will have its default value)

相关标签:
7条回答
  • 2020-11-27 06:43

    Instance variables have a default value. From the C# 3.0 specification:

    5.1.2.1 Instance variables in classes

    An instance variable of a class comes into existence when a new instance of that class is created, and ceases to exist when there are no references to that instance and the instance’s finalizer (if any) has executed.

    The initial value of an instance variable of a class is the default value (§5.2) of the variable’s type.

    For the purpose of definite assignment checking, an instance variable is considered initially assigned.

    0 讨论(0)
提交回复
热议问题