[ACCEPTED]-Conditional operator assignment with Nullable<value> types?-nullable

Accepted answer
Score: 77

The problem occurs because the conditional 9 operator doesn't look at how the value is 8 used (assigned in this case) to determine 7 the type of the expression -- just the true/false 6 values. In this case, you have a null and an 5 Int32, and the type can not be determined (there 4 are real reasons it can't just assume Nullable<Int32>).

If 3 you really want to use it in this way, you 2 must cast one of the values to Nullable<Int32> yourself, so 1 C# can resolve the type:

EmployeeNumber =
    string.IsNullOrEmpty(employeeNumberTextBox.Text)
    ? (int?)null
    : Convert.ToInt32(employeeNumberTextBox.Text),

or

EmployeeNumber =
    string.IsNullOrEmpty(employeeNumberTextBox.Text)
    ? null
    : (int?)Convert.ToInt32(employeeNumberTextBox.Text),
Score: 8

I think a utility method could help make 1 this cleaner.

public static class Convert
{
    public static T? To<T>(string value, Converter<string, T> converter) where T: struct
    {
        return string.IsNullOrEmpty(value) ? null : (T?)converter(value);
    }
}

then

EmployeeNumber = Convert.To<int>(employeeNumberTextBox.Text, Int32.Parse);
Score: 6

While Alex provides the correct and proximal 6 answer to your question, I prefer to use 5 TryParse:

int value;
int? EmployeeNumber = int.TryParse(employeeNumberTextBox.Text, out value)
    ? (int?)value
    : null;

It's safer and takes care of cases of invalid 4 input as well as your empty string scenario. Otherwise 3 if the user inputs something like 1b they 2 will be presented with an error page with 1 the unhandled exception caused in Convert.ToInt32(string).

Score: 3

You can cast the output of Convert:

EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text)
   ? null
   : (int?)Convert.ToInt32(employeeNumberTextBox.Text)

0

Score: 1
//Some operation to populate Posid.I am not interested in zero or null
int? Posid = SvcClient.GetHolidayCount(xDateFrom.Value.Date,xDateTo.Value.Date).Response;
var x1 = (Posid.HasValue && Posid.Value > 0) ? (int?)Posid.Value : null;

EDIT: Brief explanation of above, I was 6 trying to get the value of Posid (if its nonnull 5 int and having value greater than 0) in varibale 4 X1. I had to use (int?) on Posid.Value to get the conditional 3 operator not throwing any compilation error. Just 2 a FYI GetHolidayCount is a WCF method that could give null or 1 any number. Hope that helps

Score: 0

As of C# 9.0, this will finally be possible:

Target typed ?? and ?:

Sometimes 7 conditional ?? and ?: expressions don’t 6 have an obvious shared type between the 5 branches. Such cases fail today, but C# 9.0 4 will allow them if there’s a target type 3 that both branches convert to:

Person person = student ?? customer; // Shared base type
int? result = b ? 0 : null; // nullable value type

That means 2 the code block in the question will also 1 compile without errors.

EmployeeNumber =
string.IsNullOrEmpty(employeeNumberTextBox.Text)
    ? null
    : Convert.ToInt32(employeeNumberTextBox.Text),

More Related questions