方法(Funtions)

扩展方法(Extension methods (methods with thisbefore their first argument))必须在非泛型的静 态类中声明。扩展方法至少有一个参数,而且只有第一个参数能用this关键字标记。静态类本身需具有文件 作用域,多个静态类可以定义相同的扩展方法。

注意,扩展方法应谨慎使用1)用一个扩展方法扩展一个类型时,同时也扩展了派生类型;2)扩展方法可能存在版本控制问题。3)扩展方法实际是对一个静态方法的调用,CLR不会对调用方法的表达式的值进行null校验。

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

namespace Functions
{
    // 扩展属性
    // Defined in the System.Runtime.CompilerServices namespace
    // [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
    // public sealed class ExtensionAttribute : Attribute {
    // }

    public static class StringBuilderExtenstion
    {
//         public static int IndexOf(StringBuilder sb, char value)
//         {
//             for (int index = 0; index < sb.Length; index++)
//             {
//                 if (sb[index] == value)
//                 {
//                     return index;
//                 }
//             }
// 
//             return -1;
//         }

        // 定义StringBuilder类的扩展方法
        public static int IndexOf(this StringBuilder sb, char value)
        {
            for (int index = 0; index < sb.Length; index++)
            {
                if (sb[index] == value)
                {
                    return index;
                }
            }

            return -1;
        }
    }

    // 定义接口扩展方法
    public static class IEnumerableExtenstion
    {
        public static void ShowItems<T>(this IEnumerable<T> collection)
        {
            foreach (var item in collection)
            {
                Console.WriteLine(item);
            }
        }
    }

    // 定义委托扩展方法
    public static class DelegateExtenstion
    {
        public static void InvokeAndCatch<TException>(this Action<Object> d, Object o)
            where TException : Exception        
        {
            try 
            {
                d(o); 
            }
            catch (TException)
            {
                Console.WriteLine(/*TException.ToString()*/"TException");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1、类中扩展方法
            StringBuilder sb = new StringBuilder("Life is what you can make it.");        
            //int index = StringBuilderExtenstion.IndexOf(sb.Replace('i', 'j'), 'j');
            //Console.WriteLine(sb);
            int index = sb.Replace('i', 'j').IndexOf('j');

            // 2、接口中扩展方法
            "Fighting".ShowItems();
            // 隐式类型的数组功能
            new[] { "All", "is", "well" }.ShowItems(); 
            new List<int>() { 1, 2, 3 }.ShowItems();

            // 3、委托中扩展方法
            Action<Object> action = o => Console.WriteLine(o.GetType());
            action.InvokeAndCatch<NullReferenceException>(null);

            Action a = "Dream".ShowItems;
            a();

            Console.ReadKey();
        }
    }
}
方法(Funtions)_第1张图片


你可能感兴趣的:(方法(Funtions))