Why C# local variables must be initialized?

前端 未结 8 575
春和景丽
春和景丽 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:21

    A variable in a method (method scope) needs to be initialized explicitly. A variable (or 'field') at class level is initialized automatically with the default value.

    class Test
    {
       bool b; // =false
       int i; // =0
    }
    

提交回复
热议问题