c# 6.0 - Checking a nullable property after a null-conditional check of parent object -


appreciate there have been question close asking not quite here. have been checking ?. operator , came upon following scenario. situation follows:

internal class dog {    public int? age { get; set; } } 

checks in main code follows:

dog d2 = new dog() { age = 10 };   int age1 = d2.age.value; // compiles okay int age2 = d2?.age.value; // cs0266 

i know why code line age3 requesting explicit cast. d2.age being type int? , age.value being type int doesn't vary between 2 usages.

once use null-condicional operator, resulting value can null. that's why can never int.

what need is:

int age2 = (d2?.age).value; 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -