This is a new Characteristic in C#3.0

namespace ConsoleUseExtensionMethod
{
    public static class TestExtensionConsumer
    {
        public static void Main()
        {
            var s = "AlamInGamania";
            // Extension Method
            Console.WriteLine(s.WordCount());
            Console.WriteLine("{0}{1}","效果同上",WordCount(s));
            Console.ReadLine();
        }

        ///summary
        /// NonGenerics and must use static class
        ///summary
        ///<input> the input type is that want to extend </input>
        ///<return>the length of string</return>
        public static int WordCount(this string v)
        {
            return v.Length;  // Please "Note" Input parameter that The keyword "this".
        }
    }

}

If Type Built-in Functions has a same name with your func. name, C# 3.0 compiler will execute Built-in Function.

arrow
arrow
    全站熱搜

    imagefish 發表在 痞客邦 留言(0) 人氣()