Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Operator Overloading | C#

class Point
    {
        public int x;
        public int y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public static Point operator + (Point A, Point B) // overloading
        {
            Point point = new Point();

            point.x = A.x + B.x;
            point.y = A.y + B.y;

            return point;
        }
    }
    
class Program{
	private static void Main(string[] args)
    {
    	  Point point1 = new Point(1, 2);
          Point point2 = new Point(2, 3);

          Point point3 = point1 + point2;
          Console.WriteLine($"x: {point3.x} y: {point3.y}"); //prints x: 3 y: 5
          													  
    }


}
Comment

c# operator overloading

using System;

public readonly struct Fraction
{
    private readonly int num;
    private readonly int den;

    public Fraction(int numerator, int denominator)
    {
        if (denominator == 0)
        {
            throw new ArgumentException("Denominator cannot be zero.", nameof(denominator));
        }
        num = numerator;
        den = denominator;
    }

    public static Fraction operator +(Fraction a) => a;
    public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den);

    public static Fraction operator +(Fraction a, Fraction b)
        => new Fraction(a.num * b.den + b.num * a.den, a.den * b.den);

    public static Fraction operator -(Fraction a, Fraction b)
        => a + (-b);

    public static Fraction operator *(Fraction a, Fraction b)
        => new Fraction(a.num * b.num, a.den * b.den);

    public static Fraction operator /(Fraction a, Fraction b)
    {
        if (b.num == 0)
        {
            throw new DivideByZeroException();
        }
        return new Fraction(a.num * b.den, a.den * b.num);
    }

    public override string ToString() => $"{num} / {den}";
}

public static class OperatorOverloading
{
    public static void Main()
    {
        var a = new Fraction(5, 4);
        var b = new Fraction(1, 2);
        Console.WriteLine(-a);   // output: -5 / 4
        Console.WriteLine(a + b);  // output: 14 / 8
        Console.WriteLine(a - b);  // output: 6 / 8
        Console.WriteLine(a * b);  // output: 5 / 8
        Console.WriteLine(a / b);  // output: 10 / 4
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to minimum text length in textbox in c# 
Csharp :: quotes in string f# 
Csharp :: how to add a force to an object unity 
Csharp :: random mac address c# 
Csharp :: enum c# 
Csharp :: what is void onmousedown() 
Csharp :: c# xml get root attributes 
Csharp :: c# wpf timer 
Csharp :: select random from enum c# 
Csharp :: how to iterate between hour range in c# 
Csharp :: c# remove the last character of a string 
Csharp :: entity framework core db first 
Csharp :: asp.net core 6 autofac 
Csharp :: c# loop through repeater items 
Csharp :: which game engine is best 
Csharp :: get enum value c# 
Csharp :: JsonConvert.DeserializeObject options camelcasing c# .net 
Csharp :: set text in unity invisible 
Csharp :: C# unit test exception using attribrute 
Csharp :: c# null conditional operator if statement 
Csharp :: get quaternion from vector unity 
Csharp :: c# method returns multiple values 
Csharp :: get file name from stream c# 
Csharp :: C# linq mselect 
Csharp :: how to return a value in c# 
Csharp :: checkbox in c# 
Csharp :: c# standard microphone decibels 
Csharp :: c#l list<string initialize 
Csharp :: c sharp convert string time into 24 hours time 
Csharp :: convert getdate to ist c# 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =