「C#入門」インターフェースの定義(event、method、property)

サンプルコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationTest
{
    delegate void Delegate();
    interface I<T>
    {   /*event*/
        event Delegate DE;
        T this[int i]
        {
            get;
            set;
        }
  
        List<T> age
        {
            get;
            set;
        }
        void Say();
    }
    class c : I<c>
    {
        public event Delegate DE;
      public  c this[int i]
        {
            get {  return age[i]; }
            set { DE(); Say(); age[i] = value; }
        }
      public  List<c> age
        {
            get;
            set;
        }
     /*method*/
      public   void Say()
      {
          Console.WriteLine("123");
      }
    }
    class b : I<c>
    {
        public event Delegate DE;

        public c this[int i]
        {
            get { return age[i]; }
            set { DE();Say(); age[i] = value; }
        }
        public List<c> age
        {
            get;
            set;
        }
        public void Say()
        {
            Console.WriteLine("456");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            I<c> Ic = new c();
            I<c> lb = new b();
            
            Ic.DE += deC;
            lb.DE += deB;
            lb = Ic;
            lb.age = new List<c>();
            lb.age.Add(new c());
            lb[0] = new c();
            Console.WriteLine("S");
            Console.ReadKey();

        }
      static  void deC()
        {
            Console.WriteLine("c");
        }
       static void deB()
        {
            Console.WriteLine("B");
        }
    }
}

 

C#

Posted by arkgame