C# 教程 在线

2421C# 委托(Delegate)

委托多播实例:例如小明叫小张买完车票,之后接着又让他带张电影票:

// 小张类
public class MrZhang
    {
    // 其实买车票的悲情人物是小张
    public static void BuyTicket()
    {
            Console.WriteLine("NND,每次都让我去买票,鸡人呀!");
    }

    public static void BuyMovieTicket()
    {
        Console.WriteLine("我去,自己泡妞,还要让我带电影票!");
    }
}

//小明类
class MrMing
{
    // 声明一个委托,其实就是个“命令”
    public delegate void BugTicketEventHandler();

    public static void Main(string[] args)
    {
        // 这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”
        BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);

        myDelegate += MrZhang.BuyMovieTicket;
        // 这时候委托被附上了具体的方法
        myDelegate();
        Console.ReadKey();
    }
}

2420C# 索引器(Indexer)

using System;

namespace IndexerApplication{
    //1.类成员变量是数组的情况
    // class IndexedNames{
        
        // private string[] namelist = new string[size];
        // static public int size = 10;
        
        // public IndexedNames(){
            
            // for(int i = 0; i < size; i++){
                // namelist[i] = "N.A.";
            // }
        // }
        
        // public string this[int index]{
            // get{
                
                // string tmp;
                // if(index >=0 && index <=size-1){
                    // tmp = namelist[index];
                // }else{
                    // tmp="";
                // }
                // return tmp;
            // }
            // set{
                // if(index >=0 && index <=size-1){
                    // namelist[index] = value;
                // }
            // }
        // }
        
        // static void Main(string[] args){
            
            // IndexedNames names = new IndexedNames();
            // names[0] = "Zara";
            // names[1] = "Riz";
            // names[2] = "Nuha";
            // names[3] = "Asif";
            // names[4] = "Davinder";
            // names[5] = "Sunil";
            // names[6] = "Rubic";
            
            // for(int i = 0; i < IndexedNames.size; i++){
                // Console.WriteLine(names[i]);
            // }
        // }
    // }
    
        //2.类成员变量是多个基本类型
    class Employee{
        public string firstName;
        public string middleName;
        public string lastName;
        
        public string this[string index]{
            set{
                switch(index){
                    case "a":firstName = value;
                        break;
                    case "b":middleName = value;
                        break;
                    case "c":lastName = value;
                        break;
                    default: throw new ArgumentOutOfRangeException("index");
                }
            }
            get{
                switch(index){
                    case "a":return firstName;
                    case "b":return middleName;
                    case "c":return lastName;
                    default: throw new ArgumentOutOfRangeException("index");
                }
            }
        }
        
        static void Main(string[] args){
            Employee ee = new Employee();
            
            ee.firstName = "胡";
            ee.middleName = "大";
            ee.lastName = "阳";
            
            Console.WriteLine("我的名字叫: {0}{1}{2}",ee["a"],ee["b"],ee["c"]);
        }
    }
}

2419C# 索引器(Indexer)

也可在 class 实例化时自定义索引器 size。

实例:

public class IndexedNames
{
  private string[] nameList;
  public int size;
  public IndexedNames(int size)
  {
    this.size = size;
    nameList = new string[size];
    for(int i = 0;i < size; i++)
    {
      nameList[i]="N. A.";
    }
  }
  public string this[int index]
  {
    get
    {
      string temp;
      if(index>=0 && index<size)
      {
        temp = nameList[index];
      }
      else
      {
        temp = "";
      }
      return temp;
    }
    set
    {
      if(index>=0 && index<size)
      {
        nameList[index] = value;
      }
    }
  }
}
static void Main(string[] args)
{
   IndexedNames index = new IndexedNames(5);
   index[0] = "Zara";
   index[1] = "Riz";
   index[2] = "Nuha";
   index[3] = "Asif";
   index[4] = "Davinder";
   for (int i = 0; i < index.size; i++)
   {
      Console.WriteLine(index[i]);
   }
   Console.ReadKey();
}

2418C# 属性(Property)

属性如果是一个数组的话,set 中的其它代码不会执行。

using System;
namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            WordtoBit w = new WordtoBit();
            w.X[5] = true;
            foreach (bool b in w.X)
            {
                Console.WriteLine(b);
            }
            Console.ReadKey();
        }
    }
    class WordtoBit
    {
        private bool[] x = new bool[16];
        public bool[] X
        {
            get { return x; }
            set
            {
                x = value;
                Console.WriteLine("这句不能执行!");
            }
        }
    }
}

2417C# 属性(Property)

抽象属性例子代码的简化版(使用C# 6.0 语言新特性)

using System;
namespace Demo.cs
{
    class Program
    {
        public abstract class Person
        {
            public abstract string Name { get; set; }
            public abstract int Age { get; set; }
        }
        public class Student : Person
        {
            public string Code { get; set; } = "N.A";
            public override string Name { get; set; } = "N.A";
            public override int Age { get; set; } = 0;
            public override string ToString()
            {
                return $"Code:{Code},Name:{Name},Age:{Age}";
            }
        }

        static void Main(string[] args)
        {
            var s = new Student()
            {
                Code = "001",
                Name = "Zara",
                Age = 10
            };
            System.Console.WriteLine($"Student Info:={s}");

            s.Age++;
            System.Console.WriteLine($"Student Info:={s}");
        }
    }
}