Why aren't unassigned local variables automatically initialized?

前端 未结 6 1599
长发绾君心
长发绾君心 2021-01-02 06:28

It seems like there is no way to have unassigned local variables in your code or check for them, as the compiler spits out Use of unassigned local variable erro

6条回答
  •  情歌与酒
    2021-01-02 07:21

    Because what do you want in there? you want x to be zero by default and I want it to be 5...

    if they assign 0 to int(s) and all the world starts assuming so, then they will change to -1 at some point and this will break so many applications around the globe.

    in VB6 variables were assigned to something by default I think, and it was not as good as it seemed to be.

    when you use C#, or C++, you assign the value with what you want, not the compiler for you.

提交回复
热议问题