C#擴展一個現有的類

作個記錄,寫個示例this

using System;


class Rubbish
{
    public void Say() 
    { 
        Console.Write("Hello"); 
    }
}
static class RubbishExtensions
{
    public static void Say(this Rubbish argRubbish, string argString)
    {
        Console.Write(argString);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Rubbish theRubbish = new Rubbish();
        theRubbish.Say("Hey");
        Console.ReadKey(false);
    }
}
相關文章
相關標籤/搜索