I have written the following code :
object obj = 123; // Line 1
int? num = null; // Line 2
num = obj as int; // Line 3
I am getting compile time error in Line 3 as - "the as keyword must be used with reference type or nullable type('int' is a non-nullable value type)".
I want to understand:
1. Though my int is nullable, why then is it giving this error
2. How can we get the value of obj in num? Won't AS keyword work here?
Int?
Andint
are two different types.Change
To
for readability, you may even write:
I'm going to extend this a little more and say, how do you get the value from a nullable object. Well, here's one way.