C#语言入门详解(刘铁锰)---泛型

C#语言入门详解(刘铁锰)视频教程:https://www.bilibili.com/video/BV1wx411K7rb

IDE:VS2019

.NET Core 3.1

1.泛型类

实例代码:

using System;

namespace 泛型
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-------------test-------------");
            Apple ap = new Apple("red");
            Book bk = new Book("C#入门详解");
            Box<Apple> apbox = new Box<Apple>(ap);
            Console.WriteLine( apbox.Cargo.Color);
            Box<Book> bkbox = new Box<Book>(bk);
            Console.WriteLine(bkbox.Cargo.Name);
            Console.WriteLine("-------------Done-------------");
        }
    }

    class Apple  //被包装的物品类
    {
        private string _color;
        public string Color { get => _color; set => _color = value; }
        public Apple(string color)
        {
            this.Color = color;     
        }     
    }
    class Book //被包装的物品类
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public Book(string name)
        {
            this.Name = name;
        }
    }
    class Box<TCargo> //包装盒类
    {
        private TCargo _cargo;
        public TCargo Cargo { get => _cargo; set => _cargo = value; }
        public Box(TCargo cargo)
        {
            this.Cargo = cargo;   
        }
    }
}

运行结果:

-------------test-------------
red
C#入门详解
-------------Done-------------

2.泛型方法

实例代码:

using System;
using System.Collections.Generic;
namespace 泛型示例
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-----------test-----------");
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic[1] = "赵1";
            dic[2] = "赵2";
            Console.WriteLine($"#1, {dic[1]}");
            Console.WriteLine($"#2, {dic[2]}");
            IList<int> list = new List<int>();

            //使用泛型方法,避免方法成员膨胀,
            int[] a = { 1, 2, 3, 4, 5 };
            int[] b = { 1, 2, 3, 4, 5, 6 };
            double[] a1 = { 1.1, 2.2, 3.3, 4.4, 5.5 };
            double[] b1 = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };

            //int[] res1 = Zip<int>(a, b); //调用方式1
            //double[] res2 = Zip<double>(a1, b1);

            var res1 = Zip(a, b);//调用方式2
            var res2 = Zip(a1, b1);
            Console.WriteLine(string.Join(',',res1)); //打印方法1
            Console.WriteLine(string.Join(',', res2)); //打印方法1

            //foreach (var item in res1) //打印方法2
            //{
            //    Console.WriteLine(item);
            //}
            //foreach (var item in res2) //打印方法2
            //{
            //    Console.WriteLine(item);
            //}
            Console.WriteLine("-----------Done-----------");
        }

        //泛型方法
        static T[] Zip<T>(T[] a, T[] b)
        {
            T[] zipped = new T[a.Length + b.Length];
            int ai = 0;
            int bi = 0;
            int 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;
        }
    }
}

运行结果:

-----------test-----------
#1, 赵1
#2, 赵2
1,1,2,2,3,3,4,4,5,5,6
1.1,1.1,2.2,2.2,3.3,3.3,4.4,4.4,5.5,5.5,6.6
-----------Done-----------

3.泛型接口

实例代码:

using System;

namespace 泛型接口
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-----------test-----------");
            Student<int> student = new Student<int>(001, "朱元璋");
            Console.WriteLine($"name: {student.Name}, id: {student.ID}");
            Console.WriteLine("-----------Done-----------");
        }
    }

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

    //实现接口时不特化,Student<TID>为泛型类
    class Student<TID> : IUnique<TID>
    {
        public TID ID { get ; set ; }
        public string Name { get; set; }
        public Student(TID id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
    }

    //实现接口时特化,Student为非泛型类
    class Student1: IUnique<ulong>
    {
        public ulong ID { get; set; }
        public string Name { get; set; }
        public Student1(ulong id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
    }


}

运行结果:

-----------test-----------
name: 朱元璋, id: 1
-----------Done-----------

4.泛型委托

实例代码:

using System;

namespace 泛型委托
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-------------test-------------");
            //泛型委托Action
            Action action1 = new Action(SayHello);
            action1(); //调用方法1
            action1.Invoke();//调用方法2

            //泛型委托Action<>
            Action<string> action2 = new Action<string>(Say);
            Action<int, int> action3 = new Action<int, int>(Multi);
            action2.Invoke("Action<string>");
            action3.Invoke(2,2);

            //Func<>
            Func<int, int, int> func1 = new Func<int, int, int>(Add);
            Func<double, double, double> func2 = new Func<double, double, double>(Div);
            Console.WriteLine( func1.Invoke(1, 2));
            Console.WriteLine(func2.Invoke(100.1, 200.2));

            //lamda表达式
            Func<int, int ,int> funclamda = (int a, int b) => { return a + b; }; //(a, b) => { return a + b; };
            Console.WriteLine(funclamda.Invoke(100, 200));
            Console.WriteLine("-------------Done-------------");
        }

        //无返回值,无参数的方法
        static void SayHello()
        {
            Console.WriteLine("i am a action");
        }

        //无返回值,有参数的方法
        static void Say(string str)
        {
            Console.WriteLine(str);
        }
        static void Multi(int a, int b)
        {
            Console.WriteLine(a * b);
        }

        //有返回值,有参数的方法
        static int Add(int a, int b)
        {
            return a + b;
        }
        static double Div(double a, double b)
        {
            return a / b;
        }
    }


}

运行结果:

-------------test-------------
i am a action
i am a action
Action<string>
4
3
0.5
300
-------------Done-------------

5.partial类

现代.NET编程的基石之一,平时直接用到的场景不多,后台默默奉献!"不同的部分允许用不同的编程语言编写,例如WPF。

6.枚举(enum)

实例代码:

using System;

namespace Enum
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("-------------test-------------");
            Person employee = new Person(0100, "996工贼", ELevel.Employee);
            Person boss = new Person(0002, "2号老板", ELevel.Boss);
            Person bigboss = new Person(0001, "1号老板", ELevel.BigBoss);
            employee.Skill = ESkill.Csharp | ESkill.JavaScript | ESkill.Python;
            boss.Skill = ESkill.Teach | ESkill.Comminication;
            Console.WriteLine(employee.GetInfo());
            Console.WriteLine(boss.GetInfo());
            Console.WriteLine("-------------Done-------------");
        }
    }

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

        public ESkill Skill { get; set; }
        public Person(int id, string name, ELevel level)
        {
            this.ID = id;
            this.Name = name;
            this.Level = level;
        }

        public string GetInfo()
        {
            string str = $"ID: {this.ID.ToString("D4")}, \r\n"
                       + $"Name: {this.Name}, \r\n"
                       + $"Level: {this.Level},\r\n"                      
                       ;
            for (int i = 0; i < 10; i++)
            {
                ESkill temp = (ESkill)(int)Math.Pow(2, i);
                if ( (this.Skill & temp) == temp) //按位取与,结果为真则表示拥有这项技能
                {
                    str += $"Skill: {temp},\r\n";
                }
            }
            return str;
        }
    }
    enum ELevel
    {
        Employee=100,
        Manager,
        Boss,
        BigBoss,
    }

    enum ESkill
    {
        //每个成员对应的数字为2的n次方,n从0开始,依次加1
        Csharp = 1,
        Java = 2,
        JavaScript = 4,
        Python = 8,
        CPlusPlus = 16,
        C = 32,
        Teach = 64,
        Comminication = 128, 
    }
}

运行结果:

-------------test-------------
ID: 0100,
Name: 996工贼,
Level: Employee,
Skill: Csharp,
Skill: JavaScript,
Skill: Python,

ID: 0002,
Name: 2号老板,
Level: Boss,
Skill: Teach,
Skill: Comminication,

-------------Done-------------

7.结构体

实例代码:

using System;

namespace Struct
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("---------------test---------------");
            Student st1 = new Student(1, "hodor");
            st1.ShowInfo();
            Student st2 = st1;
            st2.ShowInfo();
            st2.Name = "Snow";
            st2.ID = 2;
            st2.ShowInfo();
            st1.ShowInfo(); //结构体为值类型,st2是将st1的值复制了一份,改变st2并不会影响st1
            Console.WriteLine("---------------Done---------------");
        }
    }

    struct Student : IShowInfo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        
        public Student(int id, string name)//结构体显式声明构造函,参数必须非空
        {
            this.ID = id;
            this.Name = name;
        }

        public void ShowInfo() //实现接口IShowInfo
        {
            Console.WriteLine($"ID: {this.ID}, Name: {this.Name}");
        }
    }

    interface IShowInfo //打印自身信息的接口
    { 
        void ShowInfo(); 
    }
}

运行结果:

---------------test---------------
ID: 1, Name: hodor
ID: 1, Name: hodor
ID: 2, Name: Snow
ID: 1, Name: hodor
---------------Done---------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值