在C#编程语言中,操作符重载是一种允许开发者为用户定义的类型(如类或结构体)赋予标准操作符特殊意义的技术。这使得自定义类型在行为上可以模仿内置类型,如整数(int)和字符串(string)。操作符重载主要通过声明一个以关键字operator开头的方法来实现,后跟一个操作符。C#中可以重载的操作符主要分为三类:一元操作符、二元操作符和类型转换操作符。但并非所有类型的操作符都可以被重载。下面将详细探讨每种类型的操作符重载。
一元操作符是指只需要一个操作数的操作符。参与操作的类或结构体必须包含操作符声明。一元操作符包括+、-、!、~、++、--、true和false。在重载一元操作符时,需要遵循以下规则:
简而言之,为标准C#操作符赋予针对用户定义数据类型(如类或结构体)的特殊意义的机制被称为操作符重载。
C#中的所有二元操作符都可以被重载,包括+、-、*、/、%、&、|、<<、>>。所有C#一元操作符也可以被重载,包括+、-、!、++、--。所有关系操作符也可以被重载,但只能成对重载,包括==、!=、<>、<=、>=。
例如,如果重载了+操作符,当需要计算a + b时:
这样,+操作符根据不同的数据类型被重载执行不同的操作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OperatorOverloading
{
class Rectangle
{
static void Main(string[] args)
{
Rectangle objRect1 = new Rectangle(10);
Rectangle objRect2 = new Rectangle(20);
Rectangle objRect3 = objRect1 + objRect2;
Console.WriteLine(objRect3);
Console.WriteLine(objRect3 + 15);
Console.WriteLine(objRect3 + 2.5);
Console.WriteLine(objRect3);
Rectangle objRect4 = 10;
Console.WriteLine(objRect1 == objRect4);
Console.WriteLine(objRect1 != objRect4);
Console.WriteLine(objRect1 > objRect2);
Console.WriteLine(objRect1 <= objRect4);
}
private double Side;
public Rectangle(int objRect)
{
Console.WriteLine("Int->Double->Assign to Side");
Side = (double)objRect;
}
public Rectangle(double objRect)
{
Console.WriteLine("Double->Assign to Side");
Side = objRect;
}
public override string ToString()
{
Console.WriteLine("Override object class's string");
return this.Side.ToString();
}
public static Rectangle operator + (Rectangle x, Rectangle y)
{
Console.WriteLine("Overloading + with Rectangle,Rectangle");
return new Rectangle(x.Side + y.Side);
}
public static Rectangle operator + (Rectangle x, double y)
{
Console.WriteLine("Overloading + with Rectangle,double");
return new Rectangle(x.Side + y);
}
public static Rectangle operator + (Rectangle x, int y)
{
Console.WriteLine("Overloading + with Rectangle,int");
return x + (double)y;
}
public static implicit operator Rectangle(double s)
{
Console.WriteLine("Overloading = for Rectangle objRect5=1.5 assignment");
return new Rectangle(s);
}
public static implicit operator Rectangle(int s)
{
Console.WriteLine("Overloading = for Rectangle objRect5=10 assignment");
return new Rectangle((double)s);
}
public static bool operator ==(Rectangle x, Rectangle y)
{
Console.WriteLine("Overloading == with Rectangle,Rectangle");
return x.Side == y.Side;
}
public static bool operator !=(Rectangle x, Rectangle y)
{
Console.WriteLine("Overloading != with Rectangle,Rectangle");
return !(x == y);
}
public override bool Equals(object obj)
{
return this == (Rectangle)obj;
}
public override int GetHashCode()
{
return (int)Side;
}
public static bool operator >(Rectangle x, Rectangle y)
{
Console.WriteLine("Overloading > with Rectangle,Rectangle");
return x.Side > y.Side;
}
public static bool operator <(Rectangle x, Rectangle y)
{
Console.WriteLine("Overloading < with Rectangle,Rectangle");
return x.Side < y.Side;
}
public static bool operator >=(Rectangle x, Rectangle y)
{
Console.WriteLine("Overloading >= with Rectangle,Rectangle");
return (x > y) || (x == y);
}
public double Area
{
get { return 2 * Side; }
}
}
}