Loading ...

Error: Cannot implicitly convert type 'int' to 'int?'

0 (0votes)

Many beginner developers get confused when the get this very common error like:

Error: Cannot implicitly convert type 'int' to 'int?'. An explicit conversion exists (are you missing a cast?)

Such compile time errors are common when developers try to cast from one type to the other. Sometimes the root reason for such errors might not be obvious to the new developers. Strongly typed languages do not allow implicit conversions from non-compatible types. For example take the case of int and int?. Datatype int? means that the variable belongs to a nullable type. That means it can hold a null value too, so the compiler has no way to know how to convert a type which can be null (int?) to a type which can only hold integers.

Therefore it throws this error. The way to fix this is to either change both the variables to datatype int or datatype int?.


Comments (no comments yet)

Top Posts