Jeff Reddy
Jeff Reddy

Reputation: 5670

Microsoft is casting null to a type, should I?

I'm working on some customized authentication code based on Microsoft's membership stuff. While looking into the Profile functionality, I looked into the ProfileBase class found in System.Web.dll v4.0.30319. There are a few class level variables that are declared as a type but then and then initialized to a null value that is cast 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 don't normally initialize variables that have a class level scope. I'm wondering if this is something I should be doing or what purpose it serves.

Thanks for any enlightenment.

Upvotes: 8

Views: 417

Answers (5)

Mark Brackett
Mark Brackett

Reputation: 85645

The casts are generated by your decompiler - the "official" source has null, but no cast. I don't see any benefit in adding the cast.

I don't normally initialize variables that have a class level scope. I'm wondering if this is something I should be doing or what purpose it serves.

The null (which is in the original source) seems to mostly be a style thing. Since it's normally easier to initialize a static field in the declaration, adding the null adds a bit of clarity that it's intentionally left uninitialized. It could also serve to pass an FXCop or similar check on style guidelines.

Upvotes: 1

Jakub Konecki
Jakub Konecki

Reputation: 46008

You're probably looking at the disassemblied code. This casting is probably added by the disassembler and it didn't exist in the source code.

You definitely don't have to do this kind of casting in your code.

Upvotes: 7

AMissico
AMissico

Reputation: 21684

The recommended practice is to initialize Static variables where they are declared.

Here is the actual 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; 

Upvotes: 1

RobertMS
RobertMS

Reputation: 1155

What that code is saying: initialize an area of memory to hold a type of Exception, and assign the value NULL to that variable. Since Exception is a reference type then it can be null. There is no point to casting NULL to Exception. Maybe generated code?

Upvotes: 1

Luke
Luke

Reputation: 560

There is no point to this; I believe null is always null - I certainly can't think of any examples in C based languages where it isn't. It is probably generated code rather than code that has been explicitly written like that.

Upvotes: 6

Related Questions