C# 扩展方法一

1. 何为扩展方法

    扩展方法是C#3.0引入的语法特性,是一种特殊的静态方法。它使得我们能向现有的数据类型“动态”添加方法,而不需要创建行的派生类型、重新编译或直接修改原始类型的源代码。

    注意扩展方法的要点:

     1). 扩展方法必须是静态的,并且要在一个非泛型的静态类中。

     2). 扩展方法第一个参数前必须有一个this关键字,它指明扩展方法将“附加于”哪个类型的对象上。

2. 扩展方法与原始类型同名时,扩展方法永远不会被调用  eg:Tostring

namespace ConsoleApp5
{
    public static class AV
    {
        public static string ToString<T>(this List<T> list)
        {
            var result = new StringBuilder();
            foreach (var length in list)
                result.Append(length);
            return result.ToString();
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var list = new List<int> {1, 2, 3};
            Console.WriteLine(list.ToString());
            Console.Read();
        }
    }
}
View Code

要想生效可以把toString 修改为toString1

namespace ConsoleApp5
{
    public static class AV
    {
        public static string ToString1<T>(this List<T> list)
        {
            var result = new StringBuilder();
            foreach (var length in list)
                result.Append(length);
            return result.ToString();
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var list = new List<int> {1, 2, 3};
            Console.WriteLine(list.ToString1());
            Console.Read();
        }
    }
}
View Code

object中虚方法,在它的子类中不覆盖那就只能调用父类的虚方法

   public virtual string ToString()
     {
         return this.GetType().ToString();
     }

 重新写一个数据结构才能够使用override    比如public struct DateTime中的tostring

public struct Data
{
    private double value;
    public Data(double value){this.value = value;}
    public override string ToString()
    {
        return Math.Round(this.value, 2, MidpointRounding.AwayFromZero).ToString("0.00");
    }
    public static Data operator +(Data data1, Data data2)
    {
        double d = data1.value + data2.value;
        return new Data(d);
    }
}
View Code

比如实体类中Tostring

public class People
    {
        private List<People> friends = new List<People>();

        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Brithday { get; set; }
        public People Son { get; set; }
        public People[] Friends { get { return friends.ToArray(); } }

        public void AddFriend(People newFriend)
        {
            if (friends.Contains(newFriend)) throw new ArgumentNullException("newFriend", "该朋友已添加");
            else friends.Add(newFriend);
        }
        public override string ToString()
        {
            return string.Format("Id: {0}, Name: {1}", Id, Name);
        }
        
    }
View Code

3. 扩展方法的泛型版本

    static class MyExtensionMethods
    {
         public static void Test<T>(this T obj)
        {
             Console.WriteLine(obj.ToString());
        }
    }

     在调用扩展方法时可以可以不指定泛型类型参数的具体类型。DateTime.Now.Test();但是在实际开发中,需要给出明确的参数类型。以免影响代码的可读性、

4. 目的

    提升代码的可读性,避免写一些重复性的代码了,不然有写代码写很多遍,自己救回很烦;

 
原文地址:https://www.cnblogs.com/maanshancss/p/5261006.html