4

Note: THIS IS NOT A DUPLICATE. This question asks why it is even possible to cast a null, my question is what is the difference between null and (type)null to begin with.


In this reference (from the MSDN C# reference), it is said that this code:

expression as type 

is equivalent to this code ("except that the expression variable is evaluated only one time"):

expression is type ? (type)expression : (type)null  

It made me wonder, what's the point of the null casting? How is this different from:

expression is type ? (type)expression : null  

Thanks!

Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • Possible duplicate of [Cast null value to a type](https://stackoverflow.com/questions/41219501/cast-null-value-to-a-type) – Renatas M. Sep 07 '18 at 10:37
  • 2
    The documentation states `The code is equivalent to` keyword being `equivalent`. You could remove the cast and the result would be the same as that is also equivalent to `expression as type` but why mention every possible functional equivalency? – Igor Sep 07 '18 at 10:37
  • `null` can be any type. But if you want a null string, you have to cast `null` to `string`. If you assign your expression to a `var x`, the compiler doesn't know which type `x` should be, if the assignment results in `null`. Therefore the cast. – Emaro Sep 07 '18 at 10:38
  • @Emaro `null` can be implicitly converted to any nullable type, so long as the context provides the type to convert it ti. In this case, the `(type)expression` provides sufficient context. –  Sep 07 '18 at 10:39
  • @Igor - Thanks! But is `(type)null` equivalent to `null`? If yes, why the redundancy? – Michael Haddad Sep 07 '18 at 10:44
  • @hvd, yes. However if you have some inheritance hierarchy or tupels, the type cannot always be guessed by the compiler. – Emaro Sep 07 '18 at 10:44
  • @Reniuz - Please review my edit. – Michael Haddad Sep 07 '18 at 10:46
  • The `null` value doesn't have a type, but the `null` expression might be given a type by an explicit cast, so here: `(type)null` is an expression with a type that will result in a value that doesn't have a type. The type is used by the compiler in overload resolution and similar, but once you are left with the actual value, the type disappears. – Lasse V. Karlsen Sep 07 '18 at 11:20
  • In the case of the ternary operator in this question, in this case it doesn't matter, since `null` can be safely cast to any specific reference type. – Lasse V. Karlsen Sep 07 '18 at 11:22

2 Answers2

4

Look at this example project:

static void Main(string[] args)
{
    //Error: The call is ambigious between the following methods or properties: ShowMessage(FirstClass), ShowMessage(SecondClass)
    ShowMessage(null);
    //Here it is known which type is being used
    ShowMessage((FirstClass)null);
}

private static void ShowMessage(FirstClass value)
{
    System.Console.WriteLine(value);
}

private static void ShowMessage(SecondClass value)
{
    System.Console.WriteLine(value.ToString());
}

class FirstClass { }
class SecondClass { }

Therefor, null and (FirstClass)null is not the same. With (FirstClass)null you define it explicitly as the nullable type FirstClass (class is nullable as reference type) that is null. The other null is just ... well null and must be cast implicitly to the needed nullable type.

Beware: If you'd replace

ShowMessage(NotFirstClassObj as FirstClass)

with

ShowMessage(NotFirstClassObj is Firstclass ? (FirstClass)NotFirstClassObj : null)

It still works because the compiler can still derive the correct type. But this may be the reason why it's (type)null in the documentary.

Chrᴉz remembers Monica
  • 1,829
  • 1
  • 10
  • 24
1

The documentation states The code is equivalent to keyword being equivalent. You could remove the cast and the result would be the same as that is also equivalent to expression as type but why mention every possible functional equivalency?


what is the difference between null and (type)null to begin with

There is no difference in this case. They equate to the same thing. The author probably chose to use (type)null because it more clearly conveys how as works.

Igor
  • 60,821
  • 10
  • 100
  • 175