第十六章 C# 结构体 枚举 委托

一.is 判断对象是否为某一类型 ValueType 代表值类型的基类

  int a =1;
            Console.WriteLine(a is ValueType); // true
            bool b = false;
            Console.WriteLine(b is ValueType); // true
            string c = "ccc";
            Console.WriteLine(c is ValueType); // false

二. C# 结构体(Struct)

1.结构体是值类型 关键字Struct

调用

  static int bbb;
       static People people =new People();

方法

 class People 
    { 
      public string Name { get; set; }
       public People() { 
        
        
        }
    }

2.类是引用类型

3.引用类型均隐式集成  System.Object 而值类型均隐式继承 System.ValueType

4.所有的整数类型和浮点数类型  本质都是 一个结构体

5.结构可带有方法 字段  属性 运算符 委托和事件

6.结构可实现一个或多个接口  但是不能继承其他类 和 结构体

调用

 Books books;
            books.title = "Title";
            books.book_id = 1;
            books.subject = "Title111";
            books.author = "Author";
            books.book_name = "Title113";
            Console.WriteLine(books.title);
            Console.ReadKey();

方法

  struct Books
    {
        public string title;
        public string author;
        public string subject;
        public int book_id;
        public string book_name;

    }

7.结构不支持被其他类继承

8.结构类似与密封类  不能使用virtual 或 protected

调用

 MyStruct myStruct = new MyStruct();
            myStruct.Age = 10;
            myStruct.Name = "结构体";
            myStruct.MyStructMothod();
            myStruct.IMyInterfaceMothod();

方法

   //结构体的默认修饰符是internal
struct MyStruct:IMyInterface
    {
        private int _age;
        public  int Age { get { return _age; } set { _age = value; } }
        public string Name { get; set; }
        public void MyStructMothod() {

            Console.WriteLine("MyStructMothod");
        }
        //如果定义有参构造函数 必须在 构造函数内部 初始化所有 字段和属性
        public MyStruct(int age,string name)
        { 
             _age = age;
            Name = name;
        }

        //实现接口方法

        public void IMyInterfaceMothod() {

            Console.WriteLine("IMyInterfaceMothod");
        }
        // ~MyStruct() {  } 错误: 只能在引用类型中定义析构函数
    }


 public interface IMyInterface
    {

        void IMyInterfaceMothod();
    }
    //类
    public class MyClass
    {



    }

枚举 

作用: 

枚举能够使代码更清晰  描述一组整数值  使数字更具有具体意义

枚举是值类型  包含一组数字常量的类型

关键字 enum

枚举可以在类的内部定义  可以可类平级

枚举是一组整型常量 默认是从 0 开始  也可以自己定义 范围

       

 public class MyClass {
        //每一个描述内容 都代表一个数字  默认从上到下  从0开始
        enum MyEnum
        {
            chifan,
            shuijiao,
            dadoudou,
            gouwu,
            lvyou
        }
       // 也可以自己定义范围
        enum MyEnum1 {

            chifan =10,
            shuijiao,
            dadoudou=50,
            gouwu =1000,
            lvyou
           
        }
}

  委托

1.委托就是把方法(函数)变成一种引用类型的方式

2.委托 关键字 delegate

3.委托分为定义委托和使用委托

委托写到命名空间中

1.无返回值无参数的方法


        //定义无参无返回值类型的委托
        delegate void MyDelegate();

调用

//创建委托类型的对象 用来管理某个符合要求的方法

            //1.
            MyDelegate myDelegate1 = new MyDelegate(aaa);
            //使用委托对象 调用委托的方法
            myDelegate1();

方法

 public static void aaa()
        {
            Console.WriteLine("无返回值无参数的方法");
        }

2.有返回值类型int无参数的方法

 //委托
delegate int MyDelegate2();

// 调用
 //委托的对象要和委托的类型匹配
            //2.
            MyDelegate2 myDelegate2 = new MyDelegate2(bbb);
            int tempNumber = myDelegate2();
            Console.WriteLine(tempNumber);
//方法

 public static int bbb()
        {
            Console.WriteLine("有返回值类型int无参数的方法");
            return 1;
        }

3.有无回值类型有一个int类型参数的方法

 //委托
 delegate void MyDelegate3(int a);

// 调用

             //3.
            MyDelegate3 myDelegate3 = new MyDelegate3(ccc);
            myDelegate3(10);
//方法

  public static void ccc(int a)
        {
            Console.WriteLine("有无回值类型有一个int类型参数的方法:{0}", a);
        }

4.有返回值类型string有两个string类型参数的方法

 //委托
  delegate string MyDelegate4(string a, string b);

// 调用
 //4.
            MyDelegate4 myDelegate4 = new MyDelegate4(ddd);
            string name = myDelegate4("zhanhsan", "lisi");
            Console.WriteLine(name);
//方法

       public static string ddd(string a, string b)
        {
            Console.WriteLine("有返回值类型string有两个string类型参数的方法:{0},{1}", a, b);
            return a + b;
        }

5.无返回值 有两个不同类型的参数

 //委托
 delegate void MyDelegate5(int a, string b);
// 调用
  //5
            MyDelegate5 myDelegate5 = new MyDelegate5(eee);
            myDelegate5(10, "30");
//方法

     public static void eee(int a, string b) {

            Console.WriteLine("333");

        }

6.有返回值  有两个为People类型的参数

 //委托
 delegate string MyDelegate6(People p1, People p2);
// 调用
  //6.
            MyDelegate6 myDelegate6 = new MyDelegate6(fff);
            People p1 = new People() { Name = "张三" };
            People p2 = new People() { Name = "李四" };
            string name1 =  myDelegate6(p1, p2);
//方法

    public static string fff(People p1, People p2) { 
            
               return  p1.Name+ p2.Name;
        }

//类
 public class People{
        
        public string Name { get; set; }
        public int Age { get; set; }

        public static void PeopelMothod() {

            Console.WriteLine("PeopelMothod");
        }
    }

7. 有返回值 有两个参数 字符串静态数组  list 字符串数组

 //委托
 delegate string MyDelegate7(string[] stringArray, List<string> list);
// 调用
  //7
            MyDelegate7 myDelegate7 = new MyDelegate7(Mothod7);
            string[] array = new string[2] { "zhangsan", "lisi" };
            List<string> list = new List<string>() {"wangwu","zhaoliu" };
            myDelegate7(array, list);
//方法

  //返回stirng类型  有两个参数  分别类型为 字符串静态数组  list字符串数组
        //让两个数组中的元素进行 +操作
        //定义委托来实现方法的调用
        public static string Mothod7(string[] stringArray, List<string> list) {

            string tempStr = "";
            foreach (var item in stringArray)
            {
                tempStr += item;
            }
            foreach (var item in list)
            {
                tempStr += item;
            }
            return tempStr;
        
        }


8.有返回值 有三个参数 分别是 字符串静态数组 list字符串数组 可变参数

 //委托
  delegate string MyDelegate8(string[] stringArray, List<string> list,params string[] strings);
// 调用
   //8
            MyDelegate8 myDelegate8 = new MyDelegate8(Mothod8);
            myDelegate8(array, list, "1","2","3");
//方法

   //返回类型  有3个参数  分别类型为 字符串静态数组  list字符串数组  可变参数
        //让3个参数的元素进行 +操作
        //定义委托来实现方法的调用
        public static string Mothod8(string[] stringArray, List<string> list,params string[]strings)
        {

            string tempStr = "";
            foreach (var item in stringArray)
            {
                tempStr += item;
            }
            foreach (var item in list)
            {
                tempStr += item;
            }
            foreach (var item in strings)
            {
                tempStr += item;
            }
            return tempStr;

        }

9.有返回值 People  类型  两个People类型的参数

 //委托
delegate People MyDelegate9(People p1, People p2);
//调用
  MyDelegate9 myDelegate9 = new MyDelegate9(Mothod9);
            People p11 = new People() { Name = "张三", Age = 18 };
            People p22 = new People() { Name = "李四", Age = 20 };
            People p33 =  myDelegate9(p11, p22);
            Console.WriteLine(p33.Name);
            Console.WriteLine(p33.Age);
//方法
     //返回值类型为People 有两个参数 都为People    
        //返回值的结果为 两个的属性的和
        //定义委托来实现方法的调用
        public static People Mothod9(People p1, People p2) {

            string tempName= p1.Name + p2.Name;
            int tempAag =  p1.Age+p2.Age;
            return new People() { Name = tempName, Age = tempAag };
        
        }


//类
public class People{
        
        public string Name { get; set; }
        public int Age { get; set; }

        public static void PeopelMothod() {

            Console.WriteLine("PeopelMothod");
        }
    }

 使用委托作为方法参数

例1

 delegate void MyDelegate();

 MyDelegate myDelegate = new MyDelegate(ProgramMothod1);
            ProgramMothod(10, "123", myDelegate);



 public static void ProgramMothod(int a, string b, MyDelegate myDelegate)
        {

            Console.WriteLine(a);
            Console.WriteLine(b);
            myDelegate();
           
        }


  public static void ProgramMothod1() {
            Console.WriteLine("ProgramMothod1");
        }

例2

  delegate void MyDelegate();
delegate void MyDelegate2(int a, int b);

  MyDelegate2 myDelegate2 = new MyDelegate2(ProgramMothod2);
            ProgramMothod(100, "321", myDelegate, myDelegate2, 400, 500);

 public static void ProgramMothod(int a,string b, MyDelegate myDelegate,MyDelegate2 myDelegate2 ,int c ,int d) {

            Console.WriteLine(a);
            Console.WriteLine(b);
            myDelegate();
            myDelegate2(c,d);
        }

public static void ProgramMothod2(int a ,int b) {

            Console.WriteLine(a);
            Console.WriteLine(b);

        }

例3

   delegate void MyDelegate();
    delegate void MyDelegate2(int a, int b);
    delegate int MyDelegate3(int a, int b);

 MyDelegate3 myDelegate3 = new MyDelegate3(ProgramMothod3);
            ProgramMothod(1000, "3210", myDelegate, myDelegate2, 4000, 5000, myDelegate3, 600, 800, out  tempNumber);
            Console.WriteLine(tempNumber);

  public static void ProgramMothod(int a, string b, MyDelegate myDelegate, MyDelegate2 myDelegate2, int c, int d,MyDelegate3 myDelegate3, int e ,int f,out int tempNumber)
        {

            Console.WriteLine(a);
            Console.WriteLine(b);
            myDelegate();
            myDelegate2(c, d);
            tempNumber =   myDelegate3(e, f);
        }

public static int ProgramMothod3(int a, int b) { 
        
            return a+ b;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值