Semantics - Why Initialise as null?

When the static is declared, is it not already null until it is initialised? Is there a reason for using

static MusicPlayer instance = null;

rather than

static MusicPlayer instance;

I tested it with the latter and it worked in this case. I suppose the real question is - could there be problems further down the line with this example or in other similar uses of statics where failing to initialise as null in the declaration breaks something?

This is all about programming practices. Many languages require you to initialize your variables in this way, but C# does not.

Except I’ve heard that sometimes it does, such as when the variable is local to a method.

Getting into the habit of initializing variables covers all potential cases, is useful when migrating to C++, and (arguably) makes the code more readable to humans.

But it is not necessary in C#.

1 Like

In addition to Anthony’s reply, you may also benefit from reading up on the types within C#.


See also;

Privacy & Terms