是什么:当l> 0 && l <5时为int l

时间:2019-07-09 09:23:50

标签: c#

寻找解决方案表单切换命令,我碰到了代码: (我不记得这个例子了)

So, I understand how it works
but I saw for a first time this declaratio

**int n when n ....**

what is it ?? 
Where can I read about It?
{
    "SupportedVersions": [
        {
            "EKSVersion": "1.13",
            "ContainerVersions": [
                "0.0.62",
                "0.0.63",
                "0.0.73",
                "0.0.75",
                "0.0.77"
            ]
        },
        {
            "EKSVersion": "1.14",
            "ContainerVersions": [
                "0.0.62",
                "0.0.63",
                "0.0.66",
                "0.0.67",
                "0.0.68",
                "0.0.69",
                "0.0.70",
                "0.0.71",
                "0.0.72",
                "0.0.73",
                "0.0.74",
                "0.0.75",
                "0.0.79"
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

case when is a new construct added in C# 7.0。根据文档:

  

从C#7.0开始,案例标签不再需要相互排斥,并且case标签在switch语句中出现的顺序可以确定执行哪个switch块。 when关键字可用于指定一个过滤条件,仅当该过滤条件也为true时,该条件才会导致其关联的案例标签为true。

因此,用更简单的术语来说,它允许您添加一个必须为case所选择的条件,如果您有类似(例如)的示例:

switch (val) {
    case int n when n > 5:
        return true;
    case int n:
        return false;
}

与以下内容相同:

switch (val) {
    case int n:
        if (n > 5) {
            return true;
        } else {
            return false;
        }
}