I am just curious about that "Why null propagation operator does (or can) not give any information about the type such as returning null in type of Nullable<int>
?"
Since it returns null
without type, resulting value can not pass an extension method.
class User {
public string Name { get; set; }
public User Father { get; set; }
}
var user = new User ( );
public static class Extensions {
public static string X (this string p) => "EXTENSION METHOD";
}
C# Interactive Window:
> var user = new User();
> user.Father
null
> user.Father.Name
Object reference not set to an instance of an object.
> user.Father?.Name
null
> user.Father?.Name.X()
null
EDIT:
- As @ScottChamberlain noted that
Nullable<string>
is a non compilable code. But I am not fixing it. Because the question already answered with this example, and any body can easily get the problem what I asked for.
Nullable<string>
becausestring
is a class not a struct and theT
inNullable<T>
is restricted to structs. – Scott Chamberlain Feb 7 '17 at 7:16T
is restricted to structs. Thanks for point it out. Learn new things today.-:) – Bigeyes Feb 7 '17 at 14:18