3

I ran across some code like this:

List<string> list = (List<string>)null;

Is there some reason the programmer didn't just initialize by:

List<string> list = null;

Is there a difference between the two?

Is this a habit that migrated from another programming language? Maybe C, C++, or Java?

3
  • possible duplicate of Cast a null into something? Commented Oct 27, 2014 at 18:12
  • 1
    I disagree, that is not a duplicate of my question. Commented Oct 27, 2014 at 18:24
  • As the answers state, there is no difference in your lines of code. I believe it could be the effect of a refactoring tool as well, an example could be that originally var was used instead of explicit typing, and the refactoring tool just replace var. Commented Oct 27, 2014 at 19:16

4 Answers 4

4

Is there a difference between the two?

No there is no difference.

In ILSpy, This line List<string> list = (List<string>)null; changes into List<string> list = null;

Is this a habit that migrated from another programming language?

Can't say. May be, earlier there was something different than null and then it was changed to null.

List<string> list = (List<string>) Session["List"];
Sign up to request clarification or add additional context in comments.

1 Comment

I was wondering if maybe C/C++ or Java required the casting of null. But I suppose it's possible that he replaced some code with the null.
2

In this instance, there is no practical difference, and both assignments will compile down to exactly the same MSIL opcodes. However, there is one case where casting a null does make a difference, and that's when calling an overloaded method.

class A { }
class B { }

class C
{
    public static void Foo( A value );
    public static void Foo( B value );
}

Simply calling C.Foo( null ); is ambiguous, and the compiler can't reason about which you intend to invoke, but if you cast the null first: C.Foo( (A)null );, it's now clear that you mean to call the first overload, but pass it a null instead of an instance of A.

Comments

1

There's no difference between those two lines of code. It's matter of taste I think. Although if you use casting, you can remove the type from your variable, like this:

var list = (List<string>)null;

Without casting you can't do it.

2 Comments

I don't get it. Why would you use the var keyword, and then cast to the type you want to define in the first place?
Well it's one possibility :) Matter of taste too I guess. I would prefer List<string> list = null; myself.
0

No, in the above case, you don't need the cast. You only need it in an ternary expression like this:

return somecondition ? new List<string>() : (List<string>)null;

3 Comments

I think the cast is redundant, you can do it like this: return somecondition ? new List<string>() : null;
I can't test it now, but it seems that even in your sample cast is useless
Commenters are correct, you don't need to cast, you can return null from a method that returns List<string>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.