条件表达式 Return 类型
Conditional Expression Return Type
我有两个 类 实现 IMyInterface
和一个 return 类型的异步方法 Task<IMyInterface>
.
为什么我的 return 语句 return SomeBooleanDeterminedByTheMethod ? Class1 : new Class2();
会收到编译器错误 "There is no implicit conversion type",解决此问题的最佳程序是什么?
完整方法:
public static async Task<IMyInterface> MyMethodAsync(Subclass1 Class1Child)
{
var listOfThings = new List<Tuple<int, Class1>>();
await Task.Run(() =>
{
foreach (var item in SomeCollection)
{
var DummyClass1 = new Class1() {IntProperty = 0};
var computationResult = new Tuple<int, Class1>( DummyClass1.IntProperty, DummyClass1);
listOfThings.Add(computationResult);
}
}
try
{
var returnedClass1 = (from items in listOfThings
orderby items.Item1
select items.Item2).FirstOrDefault();
return returnedClass1.BooleanProperty ? returnedClass1 : new Class2();
}
catch ... // Not relevant.
}
class Class1 : IMyInterface
{
public int IntProperty { get; set;}
public bool BooleanProperty => IntProperty % 2 == 1; // So, in my example, BooleanProperty will return false.
}
class Class2 : IMyInterface
{
// This class serves as a separate class to indicate a different Type to be used for my program.
}
interface IMyInterface { }
您可能需要强制转换三元运算符以向编译器显示预期的类型:
return SomeBooleanDeterminedByTheMethod
? (IMyInterface)new Class1()
: new Class2();
您收到该错误是因为编译器找不到将 Class1
转换为 Class2
的方法。这是由于终止语句 condition ? a : b
。编译器将尝试为 a
和 b
.
找到匹配的 return 类型
在你的情况下,你需要将其中之一转换为 IMyInterface
:
return SomeBooleanDeterminedByTheMethod ? new Class1() : (IMyInterface)new Class2();
正如 ?:
运算符的 documentation 所解释的那样:
给定条件表达式:condition ? first_expression : second_expression;
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
由于 Class1 和 Class2 不是同一类型,两者之间也不存在隐式转换,您可以将 first_expression
或 second_expression
转换为接口类型,如下所示:
return SomeBooleanDeterminedByTheMethod ? (IMyInterface)Class1 : new Class2();
然后编译器将知道表达式的计算结果应为 IMyInterface
.
我有两个 类 实现 IMyInterface
和一个 return 类型的异步方法 Task<IMyInterface>
.
为什么我的 return 语句 return SomeBooleanDeterminedByTheMethod ? Class1 : new Class2();
会收到编译器错误 "There is no implicit conversion type",解决此问题的最佳程序是什么?
完整方法:
public static async Task<IMyInterface> MyMethodAsync(Subclass1 Class1Child)
{
var listOfThings = new List<Tuple<int, Class1>>();
await Task.Run(() =>
{
foreach (var item in SomeCollection)
{
var DummyClass1 = new Class1() {IntProperty = 0};
var computationResult = new Tuple<int, Class1>( DummyClass1.IntProperty, DummyClass1);
listOfThings.Add(computationResult);
}
}
try
{
var returnedClass1 = (from items in listOfThings
orderby items.Item1
select items.Item2).FirstOrDefault();
return returnedClass1.BooleanProperty ? returnedClass1 : new Class2();
}
catch ... // Not relevant.
}
class Class1 : IMyInterface
{
public int IntProperty { get; set;}
public bool BooleanProperty => IntProperty % 2 == 1; // So, in my example, BooleanProperty will return false.
}
class Class2 : IMyInterface
{
// This class serves as a separate class to indicate a different Type to be used for my program.
}
interface IMyInterface { }
您可能需要强制转换三元运算符以向编译器显示预期的类型:
return SomeBooleanDeterminedByTheMethod
? (IMyInterface)new Class1()
: new Class2();
您收到该错误是因为编译器找不到将 Class1
转换为 Class2
的方法。这是由于终止语句 condition ? a : b
。编译器将尝试为 a
和 b
.
在你的情况下,你需要将其中之一转换为 IMyInterface
:
return SomeBooleanDeterminedByTheMethod ? new Class1() : (IMyInterface)new Class2();
正如 ?:
运算符的 documentation 所解释的那样:
给定条件表达式:condition ? first_expression : second_expression;
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
由于 Class1 和 Class2 不是同一类型,两者之间也不存在隐式转换,您可以将 first_expression
或 second_expression
转换为接口类型,如下所示:
return SomeBooleanDeterminedByTheMethod ? (IMyInterface)Class1 : new Class2();
然后编译器将知道表达式的计算结果应为 IMyInterface
.