Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and null
The spec (§7.14) says that for conditional expression b ? x : y
, there are three possibilities, either x
and y
both have a type and certain good conditions are met, only one of x
and y
has a type and certain good conditions are met, or a compile-time error occurs. Here, "certain good conditions" means certain conversions are possible, which we will get into the details of below.
Now, let's turn to the germane part of the spec:
If only one of
x
andy
has a type, and bothx
andy
are implicitly convertible to that type, then that is the type of the conditional expression.
The issue here is that in
int? number = true ? 5 : null;
only one of the conditional results has a type. Here x
is an int
literal, and y
is null
which does not have a type and null
is not implicitly convertible to an int
1. Therefore, "certain good conditions" aren't met, and a compile-time error occurs.
There are two ways around this:
int? number = true ? (int?)5 : null;
Here we are still in the case where only one of x
and y
has a type. Note that null
still does not have a type yet the compiler won't have any problem with this because (int?)5
and null
are both implicitly convertible to int?
(§6.1.4 and §6.1.5).
The other way is obviously:
int? number = true ? 5 : (int?)null;
but now we have to read a different clause in the spec to understand why this is okay:
If
x
has typeX
andy
has typeY
then
If an implicit conversion (§6.1) exists from
X
toY
, but not fromY
toX
, thenY
is the type of the conditional expression.If an implicit conversion (§6.1) exists from
Y
toX
, but not fromX
toY
, thenX
is the type of the conditional expression.Otherwise, no expression type can be determined, and a compile-time error occurs.
Here x
is of type int
and y
is of type int?
. There is no implicit conversion from int?
to int
, but there is an implicit conversion from int
to int?
so the type of the expression is int?
.
1: Note further that the type of the left-hand side is ignored in determining the type of the conditional expression, a common source of confusion here.
No implicit conversion between int and null
null does not have any identifiable type - it just needs a little prodding to make it happy: Example is shown below.
int? number = true ? 5 : (int?)null;
Or you can do
int? number = true ? 5 : null as int?;
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'
This means that return type of !Versioned.IsNumeric(text2)) ? -1 : text2
can't be narrowed to single type. It is either int (-1
) or string (text2
).
Treat both as string
int num = Conversions.ToInteger((!Versioned.IsNumeric(text2)) ? "-1" : text2);
Now, in either case the result of the conditional tenary operator will be string
Apply the condition outside
int num = Versioned.IsNumeric(text2) ? Conversions.ToInteger(text2) : -1;
- If
text2
is numeric then convert string to int - If
text2
is not numeric then use-1
Type of conditional expression cannot be determined because there is no implicit conversion between System.DateTime and null
The issue is with the ternary operation. You're changing the data type from DateTime to a nullable DateTime. Ternary operations require you to return the same data type both before and after the colon. Doing something like this would work:
someDateTimeControl.Value = (tempDate > DateTime.MinValue) ? (DateTime?)tempDate : null;
Type of conditional expression cannot be determined because there is no implicit conversion between 'int?' and 'string'
Ternary operator expects both outputs to be of same type. Use the following:
Ages = m.RelatedMultipleWorks.Count == 0 ? m.RelatedLook.Age.ToString(): m.RelatedLook.Age.ToString() + " | " + m.RelatedMultipleWorks.Select(z => z.RelatedLook.Age.ToString()).Aggregate((current, next) => current + " | " + next),
Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'null'
it happens because compiler tries to evaluate the right hand side first.
int.Parse((e.Item.FindControl("BankName_dd") as DropDownList).SelectedValue)
is int and not nullable so there is a mismatch between parameters i.e int and null
Even this would be fine if you do it. this now wakes the first parameter as nullable int
int? BankName_dd = int.Parse((e.Item.FindControl("BankName_dd") as DropDownList).SelectedValue) != -1 ? (int?)int.Parse((e.Item.FindControl("BankName_dd") as DropDownList).SelectedValue):null;
SO already answered Link
Related Topics
Call Non-Static Method in Server-Side from Client-Side Using Javsscript
Set Default Global JSON Serializer Settings
Print Webbrowser Without Previewing I.E. Single Click Print
What's the Difference Between Returning Void and Returning a Task
Converting from Ienumerable to List
Pattern for Calling Wcf Service Using Async/Await
How to Show File Copy Progress Using Fileinfo.Copyto() in .Net
Does .Net Ftpwebrequest Support Both Implicit (Ftps) and Explicit (Ftpes)
C# Inheritance and Default Constructors
How to Downgrade from Visual Studio 2012 Project to Visual Studio 2008
Modify Struct Variable in a Dictionary
Zooming Graphics Based on Current Mouse Position
Why Use Try {} Finally {} with an Empty Try Block
How to Discover the "Path" of an Embedded Resource
What Is the Fastest Way I Can Compare Two Equal-Size Bitmaps to Determine Whether They Are Identical
Could Not Find an Implementation of the Query Pattern
Synchronized Scrolling of Two Scrollviewers Whenever Any One Is Scrolled in Wpf