interface
interface 用法故名思義就是「介面」,介面不提供實作,特性像是 abstract (抽象方法)。
interface 就像可以隨意地開需求出來,但實際作法可以讓別的類別去實作;
abstract 則像是一個原形就在那邊,想要加以改良的類別就去繼承他。
一、interface 應用在方法的實作
using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare an interface instance.
            ImyInterface obj = new MyClass();
            // Call the member.
            obj.SampleMethod();
            Console.ReadKey();
        }
    }
    interface ImyInterface
    {
        void SampleMethod();
    }
    class MyClass : ImyInterface
    {
        // Explicit interface member implementation: 
        void ImyInterface.SampleMethod()
        {
            Console.WriteLine(nameof(ImyInterface.SampleMethod));
        }
    }
}
上面範例還使用了「明確介面實作」,請看黃色框框處,做法為,
以介面的名稱加上句號,後接介面所定義的方法。
二、interface 應用在屬性的實作
using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Point p = new Point(2, 3);
            Console.Write("My Point: ");
            PrintPoint(p);
            Console.ReadKey();
        }
        static void PrintPoint(IPoint p)
        {
            Console.WriteLine("x={0}, y={1}", p.x, p.y);
        }
    }
    interface IPoint
    {
        // Property signatures:
        int x { get; set; }
        int y { get; set; }
    }
    class Point : IPoint
    {
        // Fields:
        private int _myX;
        private int _myY;
        // Constructor:
        public Point(int a, int b)
        {
            _myX = a;
            _myY = b;
        }
        // Property implementation:
        public int x
        {
            get { return _myX; }
            set { _myX = value; }
        }
        public int y
        {
            get { return _myY; }
            set { _myY = value; }
        }
    }
}
三、一個實作應用在兩個 interface 上
如果類別實作了兩個介面包含相同名稱之方法,
則在類別上實作該方法會導致兩個介面都將該方法當做實作 (Implementation) 使用。
using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SampleClass sc = new SampleClass();
            IControl ctrl = (IControl)sc;
            ISurface srfc = (ISurface)sc;
            // The following lines all call the same method.
            sc.Paint();
            ctrl.Paint();
            srfc.Paint();
            Console.ReadKey();
        }
    }
    interface IControl
    {
        void Paint();
    }
    interface ISurface
    {
        void Paint();
    }
    class SampleClass : IControl, ISurface
    {
        // Both ISurface.Paint and IControl.Paint call this method. 
        public void Paint()
        {
            Console.WriteLine("Paint method in SampleClass");
        }
    }
}
上面範例未特別使用「明確介面實作」,請看黃色框框處,
其差異請和第一個範例做比較。
四、其他重點提示如下
1、C#不允許類別多重繼承,但介面可以多重繼承。
2、介面可以繼承一或多個基底介面。
3、當實作介面的類別已實作該介面的成員時,「明確實作」的成員不能經由類別執行個體存取,
需將該類別執行個體,轉型成該介面來存取。
4、介面不能包含常數、欄位、運算子、執行個體建構函式、完成項或類型。
5、介面成員會自動變成公用,且它們不能包含任何存取修飾詞。 成員也不能是 static。
6、於 C# 8.0,介面可以擁有自己的預設介面方法 (default interface methods) 了。
參考資料: