泛型、partial类、枚举、结构

文章介绍了泛型在解决类型膨胀和成员膨胀问题中的作用,通过示例展示了泛型类、接口、方法以及委托的使用,同时提到了partial类在代码组织中的应用,枚举的限制整数范围和位运算特性,以及结构体与接口的结合使用。
摘要由CSDN通过智能技术生成

泛型

  • 为什么需要:避免成员膨胀或者类型膨胀
  • 正交性:泛型类型(类/接口/委托/…)、泛型成员(属性/方法/字段/…)
  • 类型方法的推断
  • 泛型与委托、lambda表达式

partial类

  • 减少类的派生
  • partial类与Entity Framework
  • partial类与windows forms,WPF。ASP.Net core

类型膨胀
如果有1k种产品,那么就有1k种盒子,显然是有问题的

 public class Program
    {
        static void Main(string[] args)
        {
            Apple apple = new Apple() { Color = "Red" };
            AppleBox box = new AppleBox();
            box.Cargo = apple;
            Console.WriteLine(box.Cargo.Color);

            Book book = new Book() { Name = "New Book" };
            BookBox bookBox = new BookBox() { Cargo = book };
            Console.WriteLine(bookBox.Cargo.Name);
        }
    }
    class Apple
    {
        public string Color { get; set; }
    }
    class Book
    {
        public string Name { get; set; }
    }

    class AppleBox
    {
        public Apple Cargo { get; set; }
    }

    class BookBox
    {
        public Book Cargo { get; set; }
    }

成员膨胀
如果将来有1k种产品,那么这个类里面就有1k个成员

class Box
    {
        public Apple Apple { get; set; }
        public Book Book { get; set; }
    }

泛型

public class Program
    {
        static void Main(string[] args)
        {
            Apple apple = new Apple() { Color = "Red" };
            Book book = new Book() { Name = "New Book" };
            Box<Apple> box1 = new Box<Apple>() { Cargo = apple };
            Box<Book> box2 = new Box<Book>() { Cargo = book };

        }
    }
    class Apple
    {
        public string Color { get; set; }
    }
    class Book
    {
        public string Name { get; set; }
    }
    //解决了类型膨胀和成员膨胀,在main里出现的TCargo的为止都会被换
    class Box<TCargo>
    {
        public TCargo Cargo { get; set; }
    }

泛型接口

 public class Program
    {
        static void Main(string[] args)
        {
            Student<int> stu = new Student<int>();
            stu.ID = 101;
            stu.Name = "Timothy";
        }
    }

    interface IUnique<TId>
    {
        TId ID { get; set; }
    }

    class Student<TId> : IUnique<TId>
    {
        public TId ID { get; set; }
        public string Name { get; set; }
    }

泛型接口(2个参数例子)

 static void Main(string[] args)
        {
            IDictionary<int, string> dict = new Dictionary<int, string>();
            dict[1] = "Timothy";
            dict[2] = "Michael";
            Console.WriteLine($"Student #1 is {dict[1]}");
            Console.WriteLine($"Student #2 is {dict[2]}");
        }

泛型方法

static void Main(string[] args)
        {
            
        }

		//将<>放在方法名的后面
        static T[] Zip<T>(T[] a, T[] b)
        {
            T[] zipped = new T[a.Length + b.Length];
            int ai = 0, bi = 0, zi = 0;
            do
            {
                if (ai < a.Length) zipped[zi++] = a[ai++];
                if (bi < b.Length) zipped[zi++] = b[bi++];
            } while (ai < a.Length || bi < b.Length);

            return zipped;
        }

泛型委托

 static void Main(string[] args)
        {
            Func<int, int, int> func1 = Add;
            var result = func1(100, 200);
            Console.WriteLine(result);
        }

        static int Add(int a,int b)
        {
            return a + b;
        }
        static double Add(double a,double b)
        {
            return a + b;
        }

泛型委托+lamdba

static void Main(string[] args)
        {
        	//double 可以删了
            Func<double, double, double> func1 = (double a, double b) => { return a + b; };
            var result = func1(100.1, 200.2);
            Console.WriteLine(result);
        }

  • partial的理解:在一个已经固定的基类里还需要再添加一些新的内容

枚举

  • 人为限制取值范围的整数
  • 整数值的对应
  • 比特位式用法
public class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Level = Level.Employee;

            Person boss = new Person();
            boss.Level = Level.Boss;

            Console.WriteLine(boss.Level > person.Level);
            Console.WriteLine((int)Level.Employee);
            Console.WriteLine(Level.Manager);

            person.Name = "Timothy";
            person.Skill = Skill.Drive | Skill.Cook | Skill.Program | Skill.Teach;
            Console.WriteLine((person.Skill & Skill.Cook) == Skill.Cook);
        }
    }

    enum Level
    {
        Employee,
        Manager,
        Boss,
        BigBoss,
    }

    //比特位式用法
    enum Skill
    {
        Drive=1,
        Cook=2,
        Program=4,
        Teach=8,
    }

    class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public Level Level { get; set; }
        public Skill Skill { get; set; }
    }

结构体

public class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student() { ID = 101, Name = "Timtohy" };
            //拆箱、装箱
            object obj = student;
            Student student2 = (Student)obj;
            Console.WriteLine($"{student2.ID} Name:{student2.Name}");

            student.Speak();
        }
    }
    interface ISpeak
    {
        void Speak();
    }

    //结构体
    struct Student:ISpeak
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public void Speak()
        {
            Console.WriteLine($"I'm #{this.ID} student {this.Name}");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值