Get the App
SLTechnology News&Howtos  ›  Development  › 

How to overload more operators in C #

Shulou Source: shulou.com Published: 2022-06-01 01:52:36 10月05日 Update

This article introduces the knowledge of "how to overload more operators in C #". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Example

Using System

Namespace OperatorOvlApplication

{

Class Box

{

Private double length; / / length

Private double breadth; / / width

Private double height; / / height

Public double getVolume ()

{

Return length * breadth * height

}

Public void setLength (double len)

{

Length = len

}

Public void setBreadth (double bre)

{

Breadth = bre

}

Public void setHeight (double hei)

{

Height = hei

}

/ / overload + operator to add two Box objects

Public static Box operator+ (Box b, Box c)

{

Box box = new Box ()

Box.length = b.length + c.length

Box.breadth = b.breadth + c.breadth

Box.height = b.height + c.height

Return box

}

Public static bool operator = (Box lhs, Box rhs)

{

Bool status = false

If (lhs.length = = rhs.length & & lhs.height = = rhs.height

& & lhs.breadth = = rhs.breadth)

{

Status = true

}

Return status

}

Public static bool operator! = (Box lhs, Box rhs)

{

Bool status = false

If (lhs.length! = rhs.length | | lhs.height! = rhs.height

| lhs.breadth! = rhs.breadth) |

{

Status = true

}

Return status

}

Public static bool operator rhs.breadth)

{

Status = true

}

Return status

}

Public static bool operator = rhs.breadth)

{

Status = true

}

Return status

}

Public override string ToString ()

{

Return String.Format ("({0}, {1}, {2})", length, breadth, height)

}

}

Class Tester

{

Static void Main (string [] args)

{

Box Box1 = new Box (); / / declare Box1 with type Box

Box Box2 = new Box (); / / declare Box2 with type Box

Box Box3 = new Box (); / / declare Box3 with type Box

Box Box4 = new Box ()

Double volume = 0; / / Volume

/ / Box1 details

Box1.setLength (6.0)

Box1.setBreadth (7. 0)

Box1.setHeight (5.0)

/ / Box2 details

Box2.setLength (12.0)

Box2.setBreadth (13.0)

Box2.setHeight (10.0)

/ / use overloaded ToString () to display two boxes

Console.WriteLine ("Box1: {0}", Box1.ToString ())

Console.WriteLine ("Box2: {0}", Box2.ToString ())

/ / Volume of Box1

Volume = Box1.getVolume ()

Console.WriteLine ("volume of Box1: {0}", volume)

/ / Volume of Box2

Volume = Box2.getVolume ()

Console.WriteLine ("volume of Box2: {0}", volume)

/ / add two objects together

Box3 = Box1 + Box2

Console.WriteLine ("Box3: {0}", Box3.ToString ())

/ / Volume of Box3

Volume = Box3.getVolume ()

Console.WriteLine ("volume of Box3: {0}", volume)

/ / comparing the boxes

If (Box1 > Box2)

Console.WriteLine ("Box1 is greater than Box2")

Else

Console.WriteLine ("Box1 is not greater than Box2")

If (Box1 = Box2)

Console.WriteLine ("Box1 greater than or equal to Box2")

Else

Console.WriteLine ("Box1 is not greater than or equal to Box2")

If (Box1

Tags: Volume more operator operation two type Category content object knowledge utility achievement next code dilemma instance actual width situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Docker Shulou Information OPPO Reno