Microsoft injects null into a type, should I?

I am working on some custom authentication code based on Microsoft stuff. Studying Profile functionality, I looked at the ProfileBase class found in System.Web.dll v4.0.30319. There are several class level variables that are declared as a type, but then initialized to a null value, which is passed to that type.

For example,

private static Exception s_InitializeException = (Exception) null; private static ProfileBase s_SingletonInstance = (ProfileBase) null; private static Hashtable s_PropertiesForCompilation = (Hashtable) null; 

I usually do not initialize variables with a class level scope. I am wondering if this is what I should do or what purpose it fulfills.

Thanks for any enlightenment.

+8
c #
source share
5 answers

You are probably looking at disassembled code. This casting is probably added by the disassembler, and it does not exist in the source code.

You definitely don't need to do such a casting in your code.

+7
source share

There is no point in this; I believe that null always null - of course, I cannot come up with any examples in C languages ​​where this is not so. This is probably generated code, not code explicitly written in this way.

+6
source share

What this code says: initialize the memory area to hold the Exception type and assign a NULL value to this variable. Since Exception is a reference type, it can be null. It makes no sense to throw NULL in an Exception. Maybe the generated code?

+1
source share

Recommended practice is to initialize static variables where they are declared.

Here is the Microsoft code:

  ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // Static data private static SettingsPropertyCollection s_Properties = null; private static object s_InitializeLock = new Object(); private static Exception s_InitializeException = null; private static bool s_Initialized = false; private static ProfileBase s_SingletonInstance = null; private static Hashtable s_PropertiesForCompilation = null; 
+1
source share

Ghosts are generated by your decompiler - the "official" source is null , but not used. I see no benefit in adding a role.

I usually do not initialize variables with a class level scope. I am wondering if this is what I should do or what purpose it fulfills.

null (which is in the original source) seems to be mostly stylish. Since it is usually easier to initialize a static field in a declaration, adding null adds some clarity to what it intentionally left uninitialized. It can also be used to transmit FXCop or similar style rules.

+1
source share

All Articles