<pre><code>EmployeeNumber =
string.IsNullOrEmpty(employeeNumberTextBox.Text)
    ? null
    : Convert.ToInt32(employeeNumberTextBox.Text),
</code></pre>
<p>I often find myself wanting to do things like this (<code>EmployeeNumber</code> is a <code>Nullable&lt;int&gt;</code> as it's a property on a LINQ-to-SQL dbml object where the column allows NULL values).  Unfortunately, the compiler feels that</p>
<blockquote>
<p>There is no implicit conversion between 'null' and 'int'</p>
</blockquote>
<p>even though both types would be valid in an assignment operation to a nullable int on their own.</p>
<p>Using the null coalescing operator is not an option as far as I can see because of the inline conversion that needs to happen on the <code>.Text</code> string if it's not null.</p>
<p>As far as I know the only way to do this is to use an if statement and/or assign it in two steps.  In this particular case I find that very frustrating because I wanted to use the object initializer syntax and this assignment would be in the initialization block...</p>
<p>Does anyone know a more elegant solution?</p>

[{'answer_id': 75795, 'author': 'Abe Heidebrecht', 'author_id': 9268, 'author_profile': 'https://Stackoverflow.com/users/9268', 'pm_score': 2, 'selected': False, 'text': '<p>You can cast the output of Convert:</p>\n\n<pre><code>EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text)\n   ? null\n   : (int?)Convert.ToInt32(employeeNumberTextBox.Text)\n</code></pre>\n'}, {'answer_id': 75822, 'author': 'Alex Lyman', 'author_id': 5897, 'author_profile': 'https://Stackoverflow.com/users/5897', 'pm_score': 7, 'selected': True, 'text': "<p>The problem occurs because the conditional operator doesn't look at how the value is used (assigned in this case) to determine the type of the expression -- just the true/false values.  In this case, you have a <code>null</code> and an <code>Int32</code>, and the type can not be determined (there are real reasons it can't just assume <code>Nullable&lt;Int32&gt;</code>).</p>\n<p>If you really want to use it in this way, you must cast one of the values to <code>Nullable&lt;Int32&gt;</code> yourself, so C# can resolve the type:</p>\n<pre><code>EmployeeNumber =\n    string.IsNullOrEmpty(employeeNumberTextBox.Text)\n    ? (int?)null\n    : Convert.ToInt32(employeeNumberTextBox.Text),\n</code></pre>\n<p>or</p>\n<pre><code>EmployeeNumber =\n    string.IsNullOrEmpty(employeeNumberTextBox.Text)\n    ? null\n    : (int?)Convert.ToInt32(employeeNumberTextBox.Text),\n</code></pre>\n"}, {'answer_id': 75944, 'author': 'NerdFury', 'author_id': 6146, 'author_profile': 'https://Stackoverflow.com/users/6146', 'pm_score': 3, 'selected': False, 'text': '<p>I think a utility method could help make this cleaner.</p>\n\n<pre><code>public static class Convert\n{\n    public static T? To&lt;T&gt;(string value, Converter&lt;string, T&gt; converter) where T: struct\n    {\n        return string.IsNullOrEmpty(value) ? null : (T?)converter(value);\n    }\n}\n</code></pre>\n\n<p>then</p>\n\n<pre><code>EmployeeNumber = Convert.To&lt;int&gt;(employeeNumberTextBox.Text, Int32.Parse);\n</code></pre>\n'}, {'answer_id': 76049, 'author': 'user13493', 'author_id': 13493, 'author_profile': 'https://Stackoverflow.com/users/13493', 'pm_score': 3, 'selected': False, 'text': "<p>While Alex provides the correct and proximal answer to your question, I prefer to use <code>TryParse</code>:</p>\n\n<pre><code>int value;\nint? EmployeeNumber = int.TryParse(employeeNumberTextBox.Text, out value)\n    ? (int?)value\n    : null;\n</code></pre>\n\n<p>It's safer and takes care of cases of invalid input as well as your empty string scenario.  Otherwise if the user inputs something like <code>1b</code> they will be presented with an error page with the unhandled exception caused in <code>Convert.ToInt32(string)</code>.</p>\n"}, {'answer_id': 29851598, 'author': 'Sandeep', 'author_id': 1604050, 'author_profile': 'https://Stackoverflow.com/users/1604050', 'pm_score': 1, 'selected': False, 'text': '<pre><code>//Some operation to populate Posid.I am not interested in zero or null\nint? Posid = SvcClient.GetHolidayCount(xDateFrom.Value.Date,xDateTo.Value.Date).Response;\nvar x1 = (Posid.HasValue &amp;&amp; Posid.Value &gt; 0) ? (int?)Posid.Value : null;\n</code></pre>\n\n<p>EDIT: \nBrief explanation of above, I was trying to get the value of <code>Posid</code> (if its nonnull <code>int</code> and having value greater than 0) in varibale <code>X1</code>. I had to use <code>(int?)</code> on <code>Posid.Value</code> to get the conditional operator not throwing any compilation error.\nJust a FYI <code>GetHolidayCount</code> is a <code>WCF</code> method that could give <code>null</code> or any number.\nHope that helps</p>\n'}, {'answer_id': 62727948, 'author': 'Glorfindel', 'author_id': 4751173, 'author_profile': 'https://Stackoverflow.com/users/4751173', 'pm_score': 0, 'selected': False, 'text': '<p>As of <a href="https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#target-typed--and-" rel="nofollow noreferrer">C# 9.0</a>, this will finally be possible:</p>\n<blockquote>\n<h3>Target typed ?? and ?:</h3>\n<p>Sometimes conditional ?? and ?: expressions don’t have an obvious shared type between the branches. Such cases fail today, but C# 9.0 will allow them if there’s a target type that both branches convert to:</p>\n<pre><code>Person person = student ?? customer; // Shared base type\nint? result = b ? 0 : null; // nullable value type\n</code></pre>\n</blockquote>\n<p>That means the code block in the question will also compile without errors.</p>\n<pre><code>EmployeeNumber =\nstring.IsNullOrEmpty(employeeNumberTextBox.Text)\n    ? null\n    : Convert.ToInt32(employeeNumberTextBox.Text),\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/75746', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12975/']