美文网首页
switch 类型模式

switch 类型模式

作者: 落地成佛 | 来源:发表于2019-10-09 10:17 被阅读0次

类型模式

类型模式可启用简洁类型计算和转换。 使用 switch 语句执行模式匹配时,会测试表达式是否可转换为指定类型,如果可以,则将其转换为该类型的一个变量。 语法为:

C#复制

   case type varname

其中 typeexpr 结果要转换到的类型的名称,varnameexpr 结果要转换到的对象(如果匹配成功)。 自 C# 7.1 起,expr 的编译时类型可能为泛型类型参数。

如果以下任一条件成立,则 case 表达式为 true

  • expr 是与 type 具有相同类型的一个实例。

  • expr 是派生自 type 的类型的一个实例。 换言之,expr 结果可以向上转换为 type 的一个实例。

  • expr 具有属于 type 的一个基类的编译时类型,expr 还具有属于 type 或派生自 type 的运行时类型。 变量的编译时类型 是其类型声明中定义的变量类型。 变量的运行时类型 是分配给该变量的实例类型。

  • expr 是实现 type 接口的类型的一个实例。

如果 case 表达式为 true,将会明确分配 varname,并且仅在开关部分中具有本地作用域。

请注意,null 与任何类型都不匹配。 若要匹配 null,请使用以下 case 标签:

C#复制

case null:

以下示例使用类型模式来提供有关各种集合类型的信息。

C#复制

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Example
{
    static void Main(string[] args)
    {
        int[] values = { 2, 4, 6, 8, 10 };
        ShowCollectionInformation(values);

        var names = new List<string>();
        names.AddRange( new string[] { "Adam", "Abigail", "Bertrand", "Bridgette" } );
        ShowCollectionInformation(names);

        List<int> numbers = null;
        ShowCollectionInformation(numbers);
    }

    private static void ShowCollectionInformation(object coll)
    {
        switch (coll)
        {
            case Array arr:
               Console.WriteLine($"An array with {arr.Length} elements.");
               break;
            case IEnumerable<int> ieInt:
               Console.WriteLine($"Average: {ieInt.Average(s => s)}");
               break;   
            case IList list:
               Console.WriteLine($"{list.Count} items");
               break;
            case IEnumerable ie:
               string result = "";
               foreach (var e in ie) 
                  result += "${e} ";
               Console.WriteLine(result);
               break;   
            case null:
               // Do nothing for a null.
               break;
            default:
               Console.WriteLine($"A instance of type {coll.GetType().Name}");
               break;   
        }
    }
}
// The example displays the following output:
//     An array with 5 elements.
//     4 items

相关文章

  • 思科路由器配置命令

    模式类型 switch>用户模式 switch# 特权模式 switch(config)# 全局配置模式 swit...

  • switch 类型模式

    类型模式 类型模式可启用简洁类型计算和转换。 使用 switch 语句执行模式匹配时,会测试表达式是否可转换为指定...

  • Swift 中的 as、as! 和 as?

    1. 关于as 将子类转换为基类 数值类型转换 switch 中的模式匹配 Swift中可以通过switch语法检...

  • switch语句(2)

    使用事例 1、int类型switch示例 2、Enum类型switch事例 3、String类型switch示例

  • js 总结四 07-12

    switch typeof判断类型 类型转换

  • swift as,as!,as?

    Swift中的 as、as!、as? 三种操作符如何使用 类型转换、switch 语句中进行模式匹配 as!向下转...

  • Scala基础(8)- 模式匹配

    Scala强大的模式匹配机制,可以应用在switch语句、类型检查以及“析构”等场合。样本类对模式匹配进行了优化。...

  • IOS switch-case Statement requir

    1. switch参数类型 switch参数类型要求是integer type,准确来讲,是可以转换成intege...

  • Switch和Select

    一、两种类型 1)表达式Switch:case包含与switch表达式的值进行比较的表达式2)类型Switch:c...

  • Scala-模式匹配、样例类、Option类

    1、匹配模式Scala有一个十分强大的模式匹配机制,可以应用到很多场合:如switch语句、类型检查等。并且Sca...

网友评论

      本文标题:switch 类型模式

      本文链接:https://www.haomeiwen.com/subject/kwpwpctx.html