官术网_书友最值得收藏!

Operator overloading

Operator loading is a way to redefine the actual functionality of a particular operator. This is important when you're working with user-defined complex types, where the direct use of in-built operators is impossible. For instance, say, you have an object with numerous properties and you want an addition of two for these types of objects. It is not possible like this: VeryComplexObject = result = verycoplexobj1 + verycomplexobj2;. To overcome such a situation, overloading does the magic.

You cannot overload all inbuilt operators; refer to https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/overloadable-operators to see what operators are overloadable.

Let's consider the following code snippet to see how operator loading works (note that this code is not complete; refer to Github for the complete source code):

public struct Coordinate 
{ 
//code omitted 
 
public static Coordinateoperator +(Coordinate coordinate1, Coordinate coordinate2) =>; 
new Coordinate(coordinate1._xAxis + coordinate2._xAxis, coordinate1._yAxis + coordinate2._yAxis); 
public static Coordinateoperator-(Coordinate coordinate1, Coordinate coordinate2) => 
new Coordinate(coordinate1._xAxis - coordinate2._xAxis, coordinate1._yAxis - coordinate2._yAxis); 
public static Coordinateoperator *(Coordinate coordinate1, Coordinate coordinate2) => 
new Coordinate(coordinate1._xAxis * coordinate2._xAxis, coordinate1._yAxis * coordinate2._yAxis); 
//code omitted 
 
public static booloperator ==(Coordinate coordinate1, Coordinate coordinate2) =>; 
        coordinate1._xAxis == coordinate2._xAxis && coordinate1._yAxis == coordinate2._yAxis; 
 
public static booloperator !=(Coordinate coordinate1, Coordinate coordinate2) => !(coordinate1 == coordinate2); 
 
//code omitted 
 
public double Area() => _xAxis * _yAxis; 
 
public override string ToString() =>$"({_xAxis},{_yAxis})"; 
}

In the preceding code, we have a new type coordinate, which is a surface of x axis and y axis. Now if we want to apply some operations, that is not possible with the use of inbuilt operators. With the help of operator overloading, we enhance the actual functionality of inbuilt operators. The following code is the consumed coordinate type:

private static void OperatorOverloadigExample() 
{ 
WriteLine("Operator overloading example\n"); 
Write("Enter x-axis of Surface1: "); 
var x1 = ReadLine(); 
Write("Enter y-axis of Surface1: "); 
var y1 = ReadLine(); 
Write("Enter x-axis of Surface2: "); 
var x2= ReadLine(); 
Write("Enter y-axis of Surface2: "); 
var y2= ReadLine(); 
 
var surface1 = new Coordinate(Convert.ToInt32(x1),Convert.ToInt32(y1)); 
var surface2 = new Coordinate(Convert.ToInt32(x2),Convert.ToInt32(y2)); 
WriteLine(); 
Clear(); 
WriteLine($"Surface1:{surface1}"); 
WriteLine($"Area of Surface1:{surface1.Area()}"); 
WriteLine($"Surface2:{surface2}"); 
WriteLine($"Area of Surface2:{surface2.Area()}"); 
WriteLine(); 
WriteLine($"surface1 == surface2: {surface1==surface2}"); 
WriteLine($"surface1 < surface2: {surface1 < surface2}"); 
WriteLine($"surface1 > surface2: {surface1 > surface2}"); 
WriteLine($"surface1 <= surface2: {surface1 <= surface2}"); 
WriteLine($"surface1 >= surface2: {surface1 >= surface2}"); 
WriteLine(); 
var surface3 = surface1 + surface2; 
WriteLine($"Addition: {nameof(surface1)} + {nameof(surface2)} = {surface3}"); 
WriteLine($"{nameof(surface3)}:{surface3}"); 
WriteLine($"Area of {nameof(surface3)}: {surface3.Area()} "); 
WriteLine(); 
WriteLine($"Substraction: {nameof(surface1)} - {nameof(surface2)} = {surface1-surface2}"); 
WriteLine($"Multiplication: {nameof(surface1)} * {nameof(surface2)} = {surface1 * surface2}"); 
WriteLine($"Division: {nameof(surface1)} / {nameof(surface2)} = {surface1 / surface2}"); 
WriteLine($"Modulus: {nameof(surface1)} % {nameof(surface2)} = {surface1 % surface2}"); 
} 

In the preceding code snippet, we declared a variable of our struct Coordinate and call operators for various operations. Note that by overloading, we have changed the actual behavior of the operator, for instance, the add (+) operator, which generally adds two numbers, but with the implementation here, the add (+) operator gives the sum of two surfaces. The complete code produces the following result:

主站蜘蛛池模板: 鄢陵县| 台东市| 出国| 广河县| 高州市| 方山县| 上蔡县| 盐边县| 高阳县| 杭州市| 新乡县| 杂多县| 桐庐县| 齐齐哈尔市| 赫章县| 徐闻县| 阿坝县| 唐山市| 翁牛特旗| 遂溪县| 巴中市| 岐山县| 瑞丽市| 安阳县| 松滋市| 正镶白旗| 垣曲县| 合作市| 博兴县| 新竹县| 宾川县| 肇州县| 呼图壁县| 嵩明县| 望城县| 同江市| 耒阳市| 门头沟区| 建湖县| 错那县| 阿鲁科尔沁旗|