• 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,
                };
            }
  • 相关阅读:
    Andriod一段时间未操作页面,系统自动登出
    Error:Execution failed for task ':app:clean'
    Handler的postDelayed(Runnable, long)
    Android Studio快捷键大全
    Cookie、Session、Token的区别
    CentOS 7 上安装jdk
    CentOS 7 上搭建nginx来部署静态网页
    PyCharm如何设置 “ctrl+滚轮” 实现字体的放大和缩小
    HTTP和HTTPS
    性能测试思想(What is performance testing?)
  • 原文地址:https://www.cnblogs.com/Celebrator/p/12420171.html
Copyright © 2020-2023  润新知