简体   繁体   中英

C# type narrowing with if condition of nullable int

The compiler complains that int? cannot be converted to int . I come from a typescript background where this would work because the compiler understands that row can't be null . Am I missing something or do I have to explicitly convert row to int with int test = (int) row ?

int? row = GetRowOfCode(excel, code);
if (row != null)
{
  int test = row;
}

In C#, once a variable is declared, it keeps the type it's declared as.

With reference types, like string , the nullable syntax is only supported by attributes and inference at compile-time, so string? is effectively the same as string besides some compiler hints. This would allow syntax like yours to work if you were working with a reference type.

But with value types, like int , the nullable type is literally a completely different type at run-time, so the value needs to be extracted from an int? in order to treat it like an int . You have to use a completely different variable to hold a strongly-typed int? versus int . That might look something like this:

if (row != null) // or row.HasValue
{
    int test = row.Value;
    // use `test`
}

With newer pattern matching syntax, you can do this instead:

if (row is int test)
{
   // use `test`
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM