Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ternary expressions with an explicit null and another type return a nullable #2853

Closed
realbart opened this issue Oct 3, 2019 · 1 comment

Comments

@realbart
Copy link

realbart commented Oct 3, 2019

Expressions like true ? 1 : null currently throw an exception:

error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '<null>'

Therefore, the codebase I inherited contains many instances of:

Housenumber = int.TryParse(organization.HouseNumber, out var parsed) ? parsed : default(int?)

or

Housenumber = int.TryParse(organization.HouseNumber, out var parsed) ? (int?)parsed : null

or

Housenumber = int.TryParse(organization.HouseNumber, out var parsed) ? parsed : (int?)null

I'd like to be able to write:

Housenumber = int.TryParse(organization.HouseNumber, out var parsed) ? parsed : null
@yaakov-h
Copy link
Member

yaakov-h commented Oct 3, 2019

Duplicate of #33 / #2460 / #2823

@gafter gafter closed this as completed Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants