Should I always/ever/never initialize object fields to default values?

前端 未结 17 2126
臣服心动
臣服心动 2020-12-09 14:49

Code styling question here.

I looked at this question which asks if the .NET CLR will really always initialize field values. (The answer is yes.) But it

17条回答
  •  爱一瞬间的悲伤
    2020-12-09 15:46

    I think that it makes sense to initialize the values if it clarifies the developer's intent.

    In C#, there's no overhead as the values are all initialized anyway. In C/C++, uninitialized values will contain garbage/unknown values (whatever was in the memory location), so initialization was more important.

提交回复
热议问题