欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#重载运算符详解

程序员文章站 2023-12-17 17:37:04
本文较为详细的描述了重载运算符的方法。一般来说,重载运算符在实际的项目开发中会经常的用到,但如果某些自定义类型通过简短几行代码重载一些常用的运算符(如:+-*/),就能让编...

本文较为详细的描述了重载运算符的方法。一般来说,重载运算符在实际的项目开发中会经常的用到,但如果某些自定义类型通过简短几行代码重载一些常用的运算符(如:+-*/),就能让编程工作带来方便;重载运算符就是告诉编译器+-*/等运算符对于自定义类型进行什么样的操作,在代码中需要注意几点。

一、尽可能的不要改变运算符本身的含义

二、所有的运算符重载都必须声明为public和static

三、不同于扩展方法,所重载的方法必须是在被重载的类型内部,且用关键字operator

c#中的两个字符串相加,实际上是连接两个字符串,假如有两个employeedetail类型相加得到一个employeecollection集合,如:

employeedetail a,b;

....

employeecollection collection = a+b;

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

[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();
   }
 }

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

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();
}

运行结果如下图所示:

C#重载运算符详解

全部代码,里面还包含其他运算符的重载,这里就不再介绍了,赶紧动手测试一下吧:

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();
    }
  }
}

上一篇:

下一篇: