Initializing

I noticed at 5:08 that he says that if you don’t initialize to 0, it will default to 0. Then later he said it will default to something. I just wanted to let everyone know that it will not always default to 0. It will default to what was previously stored in that memory cell. As a good programming style, always initialize.

Are you sure about this? Default values of C# types - C# reference | Microsoft Docs mentions that both integer and floating point built-in types get initialised to zero? But maybe I’m missing something, technical details of C# aren’t my strong suit.

Nonetheless, I don’t argue with good programming style and I also prefer to explicitly initialise to avoid any doubt; in C++ it is required as there it’s indeed just taking whatever was stored at a given address (at least, in any release build; debug builds are yet another story :slight_smile: )

There could be a possibly I was thinking about C++?

It’s possible :slight_smile: I can definitely confirm that C++ behaves as you have described, taking whatever data is in a memory cell. And in general, even if you’re 100% sure that it initializes to 0 in C#, I would also prefer to still do an explicit 0-initialization, even if it’s only for readability (I guess, as a bonus, if you switch between programming languages, it helps if you already have the habit of explicitly initializing to 0, explicitly marking your private methods private, etc.)

Privacy & Terms