I get the following compilation error with the following source code:
Compilation Error:
Type of conditional expression cannot be determined because there is no implicit conversion between ” and ‘MyEnum’
Source Code
public enum MyEnum { Value1, Value2, Value3 } public class MyClass { public MyClass() {} public MyEnum? MyClassEnum { get; set; } } public class Main() { object x = new object(); MyClass mc = new MyClass() { MyClassEnum = Convert.IsDBNull(x) : null ? (MyEnum) Enum.Parse(typeof(MyEnum), x.ToString(), true) }; }
How can I resolve this error?
I think you just need to cast the result of
Enum.ParsetoMyEnum?. This is the case with nullable ints at least. E.g.:So: