C#では、単一のステートメントで次のことを行うためのシンタックスシュガーはありますか(基本的に、条件付きリターン):
public SomeBaseType MyFunction()
{
// Can the two statements below be combined into one?
SomeBaseType something = SomeFunction();
if ( something != null ) { return something; }
// End of statements regarding this question.
// Do lots of other statements...
return somethingElseThatIsADerivedTypeThatDoesntMatter;
}
いいえ、(条件に基づいて)戻るか、メソッドで続行する「条件付きreturnステートメント」はありませんが、私も時々望んでいました。あなたは書くことができます:
public SomeBaseType MyFunction()
{
return SomeFunction() ?? LocalMethod();
SomeBaseType LocalMethod()
{
// Do lots of other statements...
return somethingElseThatIsADerivedTypeThatDoesntMatter;
}
}
...しかし、それは本当に明確ではありません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加