zoukankan      html  css  js  c++  java
  • c# 8 特性

    属性匹配模式

        public class Teacher
        {
            public string Name { get; set; }
            public string Subject { get; set; }
            public void Deconstruct(out string name,out string subject)
            {
                name = Name;
                subject = Subject;
            }
        }
    
    
        public class Student
        {
            public Student(string name,int grade,Teacher teacher)
            {
                Name = name;
                Grade = grade;
                Teacher = teacher;
            }
            public string Name { get; set; }
            public int Grade { get; set; }
            public Teacher Teacher { get; set; }
            public void Deconstruct(out string name, out int grade, out Teacher teacher)
            {
                name = Name;
                grade = Grade;
                teacher = Teacher;
            }
    
    
            public bool Is5GradeStudent(Student student)
            {
                return student is Student(_, 5, Teacher(_, "物理"));
            }
        }
    
    
            static void Main(string[] args)
            {
                Student stu = new Student("adsfsa", 5, new Teacher()
                {
                    Name = "张超",
                    Subject = "物理"
                });
    
                bool sss= stu.Is5GradeStudent(stu);
                Console.WriteLine(sss.ToString());
            }

    还可以这么写

            public bool Is5GradeStudent(Student student)
            {
                return student is { Grade: 5, Teacher: { Subject: "物理" } };
            }
            public bool Is5GradeStudent(object obj)
            {
                return obj is Student s && s is { Grade: 5, Teacher: { Subject: "物理" } };
            }

    Switch 

        public class Circle
        {
            public Circle(int redius) => Redius = redius;
            public int Redius { get; set; }
        }
        public class Rectangle
        {
            public Rectangle(int length, int width) => (Length, Width) = (length, width);
            public int Length { get; set; }
            public int Width { get; set; }
        }
        public class Triangle
        {
            public int SideA { get; set; }
            public int SideB { get; set; }
            public int SideC { get; set; }
        }
    
            public static string ShapeInfo(object shape)
            {
                string info = shape switch
                {
                    Rectangle r => r switch
                    {                    
                        _ when r.Length==r.Width =>"正方形",
                        _ => $"矩形,长={r.Length},宽={r.Width}",
                    },
                    Circle { Redius: 1 } => "小圆形",
                    Circle c => $"圆形,半径={c.Redius}",
                    Triangle t => $"三角形,A={t.SideA},B={t.SideB},C={t.SideC}",
                    _ => "未知"
                };
                return info;
            }
        public enum Color
        {
            UnKnown,
            Red,
            Green,
            Blue,
            Purple,
            Orange,
            Brown,
            Yellow
        }
    
            public static Color MixColor(Color c1,Color c2)
            {
                return (c1, c2) switch
                {
                    (Color.Blue, Color.Red) => Color.Purple,
                    (Color.Red, Color.Blue) => Color.Purple,
                    (_, _) when c1 == c2=> c1,
                    _ => Color.UnKnown,
                };
            }
  • 相关阅读:
    idea html,js修改不用重启进程
    opencv rtsp 人脸识别
    The system is running in low-graphics mode UB16
    阿里云ecs 增加虚拟网卡
    rtsp
    mysql5.7报err 1055错误 sql_mode=only_full_group_by
    python 生成requirements.txt
    Linux 保护文件 不给修改
    logback logback.xml常用配置详解(三) <filter>
    logback 常用配置详解(二) <appender>
  • 原文地址:https://www.cnblogs.com/Celebrator/p/12420171.html
Copyright © 2011-2022 走看看