重载运算符

重载运算符在实际的工做中很用到,但若是某些自定义类型经过简短几行代码重载一些经常使用的运算符(如:+-*/),就能让编程工做带来方便;重载运算符就是告诉编译器+-*/等运算符对于自定义类型进行什么样的操做,在代码中须要注意几点。正则表达式

1、尽量的不要改变运算符自己的含义编程

2、全部的运算符重载都必须声明为public和staticide

3、不一样于扩展方法,所重载的方法必须是在被重载的类型内部,且用关键 operator测试

C#中的两个字符串相加,其实是链接两个字符串,假若有两个EmployeeDetail类型相加获得一个EmployeeCollection集合,如:spa

EmployeeDetail a,b;code

....orm

EmployeeCollection collection = a+b;对象

当编译器遇到上面的代码时就会自动调用EmployeeDetail类上标有operator +的静态方法,并将两个操做数a和b做为参数传递给对于的方法,该方法须要方法一个值赋给collection,假设EmployeeDetail类有三个属性分别是FirstName,MiddleName,LastName,还重写了ToString方法返回一个链接这三个名称的字符串,代码如:blog

  [Serializable]
    public class EmployeeDetail
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { set;get; }
        public override string ToString()
        {
            return string.Format("{0}{1}{2}{3}{4}", FirstName, string.IsNullOrWhiteSpace(MiddleName) ? null : "."
                , MiddleName
                , string.IsNullOrWhiteSpace(LastName) ? null : ".",
                LastName).Trim();
        }
    }

 

下面的代码为“+”运算符提供支持的运算符重载:ci

 public static EmployeeCollection operator +(EmployeeDetail a, EmployeeDetail b)
        {
            return new EmployeeCollection() { a, b };
        }

OK,给EmployeeDetail类加上这样的一个方法以后,咱们就能够像下面那个写代码了:

EmployeeCollection collection = new EmployeeDetail(){FirstName="Jackson",LastName="Bruce"}  + new EmployeeDetail(){FirstName="Michael",LastName="Jackson"} ;

可是这样还不够完美,假设a,b,c都是EmployeeDetail类型,下面的代码会抛出一个编译错误:

EmployeeCollection collection = a + b + c;

为何编译不经过呢?你们都知道除了赋值运算符外表达式是从左到右执行的,a+b返回的是EmployeeCollection类型,EmployeeCollection类型并无重载“+”运算符,编译器不知道要执行什么操做,因此咱们还有下面的两个方法:

        public static EmployeeCollection operator +(EmployeeCollection collection, EmployeeDetail a)
        {
            collection.Add(a);
            return collection;
        }
        public static EmployeeCollection operator +(EmployeeDetail a, EmployeeCollection collection)
        {
            return collection + a;
        }

 

 

这看起来彷佛已经很完美了,但咱们还能够作得更好一些,好比要将字符串“Jackson.Bruce”直接隐式转换为EmployeeDetail类型,也就是说能够将“Jackson.Bruce"这种格式的字符串直接赋给EmployeeDetail类型的对象,如:EmployeeDetail employee= “Jackson.Bruce",那么就须要重载隐式类型转换运算符了,代码以下:

/// <summary>
        /// 隐式类型转换
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static implicit operator EmployeeDetail(string name)
        {

       /// 其实在这里能够写一个正则表达式检查name的字符串格式是否合法,若是不合法就抛出异常
       ///
string[] arr; return string.IsNullOrWhiteSpace(name) ? null : new EmployeeDetail() { FirstName = (arr = name.Trim().Split('.'))[0] , LastName = arr.Length > 1 ? arr[arr.Length > 2 ? 2 : 1] : null, MiddleName = arr.Length > 2 ? arr[1] : null }; } public static EmployeeCollection operator +(EmployeeDetail a, string b) { return new EmployeeCollection() { a, b }; }

 

 

看到这里您是否是火烧眉毛地想试试看,OK写个控制台程序来测试一下:

        static void Main(string[] args)
        {
            EmployeeDetail employee = "Jackson.Bruce";
            Console.WriteLine("FirstName={0},MiddleNam={1},LastName={2}", employee.FirstName, employee.MiddleName, employee.LastName);
            Console.WriteLine("toString={0}", employee);
            Console.WriteLine();

            EmployeeCollection collection = "Michael.Jackson" + employee;

            collection += "Bruce.Lee";
            foreach (var e in collection)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine();

            collection -= employee;

            foreach (var e in collection)
            {
                Console.WriteLine(e);
            }


            Console.WriteLine("===end===");
            Console.Read();
        }

 

 

运行结果

 

 

所有代码,里面还包含其余运算符的重载,这里就再也不介绍了,赶忙试试吧:

 

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

namespace 重载运算符
{
    [Serializable]
    public class EmployeeDetail
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { set;get; }
        public static EmployeeCollection operator +(EmployeeDetail a, EmployeeDetail b)
        {
            return new EmployeeCollection() { a, b };
        }

      
       
        public static EmployeeCollection operator +(EmployeeCollection collection, EmployeeDetail a)
        {
            collection.Add(a);
            return collection;
        }
        public static EmployeeCollection operator +(EmployeeDetail a, EmployeeCollection collection)
        {
            return collection + a;
        }
        /// <summary>
        /// 隐式类型转换
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static implicit operator EmployeeDetail(string name)
        {
            string[] arr;
            return string.IsNullOrWhiteSpace(name) ? null :
                new EmployeeDetail()
                {
                    FirstName = (arr = name.Trim().Split('.'))[0]
                    ,
                    LastName = arr.Length > 1 ? arr[arr.Length > 2 ? 2 : 1] : null,
                    MiddleName = arr.Length > 2 ? arr[1] : null
                };
        }
        public static EmployeeCollection operator +(EmployeeDetail a, string b)
        {
            return new EmployeeCollection() { a, b };
        }
        public override string ToString()
        {
            return string.Format("{0}{1}{2}{3}{4}", FirstName, string.IsNullOrWhiteSpace(MiddleName) ? null : "."
                , MiddleName
                , string.IsNullOrWhiteSpace(LastName) ? null : ".",
                LastName).Trim();
        }
    }
    public class EmployeeCollection : List<EmployeeDetail>
    {
        public static EmployeeCollection operator +(EmployeeCollection a, string b)
        {
            a.Add(b);
            return a;
        }
        public static EmployeeCollection operator +(string b, EmployeeCollection a)
        {
            return a + b;
        }

        public static EmployeeCollection operator -(EmployeeCollection a,  EmployeeDetail b)
        {
            a.Remove(b);
            return a;
        }
    }
  


    class Program
    {
        static void Main(string[] args)
        {
            EmployeeDetail employee = "Jackson.Bruce";
            Console.WriteLine("FirstName={0},MiddleNam={1},LastName={2}", employee.FirstName, employee.MiddleName, employee.LastName);
            Console.WriteLine("toString={0}", employee);
            Console.WriteLine();

            EmployeeCollection collection = "Michael.Jackson" + employee;

            collection += "Bruce.Lee";
            foreach (var e in collection)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine();

            collection -= employee;

            foreach (var e in collection)
            {
                Console.WriteLine(e);
            }


            Console.WriteLine("===end===");
            Console.Read();
        }
    }
}
相关文章
相关标签/搜索