I found in Jon Skeet's book an example of use as a type operator that allows null .
using System;
class A
{
static void PrintValueAsInt32(object o)
{
int? nullable = o as int?;
Console.WriteLine(nullable.HasValue ?
nullable.Value.ToString() :
"null");
}
static void Main()
{
PrintValueAsInt32(5);
PrintValueAsInt32("some string");
}
}
I can’t understand why I can’t write int? nullable = (int?)o? When I try to do this, I get an exception.
source
share