(022)c# is 的用法

版本

C# 7.0 开始

使用方法

类型模式

  1. 描述:用于测试表达式是否可转换为指定类型,如果可以,则将其转换为该类型的一个变量。
  2. 格式:expr is type varnamevarname 是类型匹配后,赋值的变量名称。
  3. 例子:
    using System;
    
    public class Employee : IComparable
    {
        public String Name { get; set; }
        public int Id { get; set; }
    
        public int CompareTo(Object o)
        {
            if (o is Employee e)
            {
                return Name.CompareTo(e.Name);
            }
            throw new ArgumentException("o is not an Employee object.");
        }
    }
    

常量模式

  1. 描述:用于测试表达式计算结果是否为指定的常数值。
  2. 格式:expr is constant, constant 的可以是下面的表达式:
    • 文字值。
    • 一个枚举常量。
    • 已声明 const 变量的名称。
  3. 例子:
    using System;
    
    public class Dice
    {
        Random rnd = new Random();
        public Dice()
        {
        }
        public int Roll()
        {
            return rnd.Next(1, 7);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var d1 = new Dice();
            ShowValue(d1);
        }
    
        private static void ShowValue(object o)
        {
            const int HIGH_ROLL = 6;
    
            if (o is Dice d && d.Roll() is HIGH_ROLL)
                Console.WriteLine($"The value is {HIGH_ROLL}!");
            else
                Console.WriteLine($"The dice roll is not a {HIGH_ROLL}!");
         }
    }
    
  4. 可以执行 null 检查:
    expr is null
    

var 模式

  1. 描述:始终匹配成功,可将表达式的值绑定到新局部变量。
  2. 格式:expr is var varname
  3. 如果 exprnullis 表达式也为 truevarnamenull
  4. 示例:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    class Program
    {
        static void Main()
        {
            int[] testSet = { 100271, 234335, 342439, 999683 };
    
            var primes = testSet.Where(n => Factor(n).ToList() is var factors
                                        && factors.Count == 2
                                        && factors.Contains(1)
                                        && factors.Contains(n));
    
            foreach (int prime in primes)
            {
                Console.WriteLine($"Found prime: {prime}");
            }
        }
    
        static IEnumerable<int> Factor(int number)
        {
            int max = (int)Math.Sqrt(number);
            for (int i = 1; i <= max; i++)
            {
                if (number % i == 0)
                {
                    yield return i;
                    if (i != number / i)
                    {
                        yield return number / i;
                    }
                }
            }
        }
    }
    // The example displays the following output:
    //       Found prime: 100271
    //       Found prime: 999683
    
    这里作为临时变量。

[1] is 的用法

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值