Why C# local variables must be initialized?

前端 未结 8 579
春和景丽
春和景丽 2020-12-03 22:28

I am reading MCTS Self Paced Training Kit (70-536) Edition 2 and in the 1st chapter we have the following.

How to Declare a Value Type Variable To

8条回答
  •  一生所求
    2020-12-03 23:19

    This statement really should be elaborated to indicate that, although a local variable can be declared without assigning it a value, it cannot be used until it has been assigned an initial value:

    The constructor assigns a default value (usually null or 0) to the new instance, but you should always explicitly initialize the variable within the declaration...

提交回复
热议问题