-
Consider the following:
Why is it so? The To make things worse, VS suggests removing parentheses from |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You want: string? s = a?.X.ToString(); This behavior is by design. The null-propagating operator bails immediately on the first |
Beta Was this translation helpful? Give feedback.
-
You might need to chain if class A { public int? X }
string? s = a?.X?.ToString(); That basically expands into: string? s;
if (a != null)
{
int? temp = a.X;
if (temp != null)
{
s = temp.Value.ToString();
}
else
{
s = null;
}
}
else
{
s = null;
} |
Beta Was this translation helpful? Give feedback.
You might need to chain if
X
was itself nullable, e.g.:That basically expands into: