小编use*_*035的帖子

是否有效:C#多个三元运算符+如果不匹配则抛出

您是否发现以下C#代码清晰可辨?

private bool CanExecuteAdd(string parameter) {
    return
        this.Script == null ? false
        : parameter == "Step" ? true
        : parameter == "Element" ? this.ElementSelectedInLibrary != null && this.SelectedStep != null
        : parameter == "Choice" ? this.SelectedElement != null
        : parameter == "Jump" ? this.SelectedStep != null
        : parameter == "Conditional jump" ? false
        : false.Throw("Unknown Add parameter {0} in XAML.".F(parameter));
}
Run Code Online (Sandbox Code Playgroud)

其中Throw定义为:

public static T Throw<T>(this T ignored, string message) {
    throw new Exception(message);
}
Run Code Online (Sandbox Code Playgroud)

我知道这不是惯用的C#.但是,你能在第一眼还是第二眼看到它?还是我偏离了太远?

c# coding-style exception conditional-operator

34
推荐指数
6
解决办法
9315
查看次数