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 :: dotnet new api 
Csharp :: difference between class and struct in c# 
Csharp :: ef rollback migration 
Csharp :: httpcontext in .net standard 
Csharp :: c# write to output window 
Csharp :: unity sound 
Csharp :: c# dictionary values to list 
Csharp :: c# int array 
Csharp :: windows form textbox password 
Csharp :: wpf messagebox result 
Csharp :: how to create empty text file in c# 
Csharp :: 1 line if c# 
Csharp :: if file exist rename c# 
Csharp :: length of a string c# 
Csharp :: c# string contains 
Csharp :: checking if character is a digit or not in c# 
Csharp :: difference between boxing and unboxing in java 
Csharp :: get what week of the month c# 
Csharp :: update listbox using class c# 
Csharp :: c# copy files from one folder to another 
Csharp :: C# async to sync 
Csharp :: unity camera follow with lerp 
Csharp :: c# convert list to array function 
Csharp :: how to define a function in c# 
Csharp :: c# xml to json 
Csharp :: how to compare datetime in c# 
Csharp :: create new object from generic c# 
Csharp :: draw on picturebox c# 
Csharp :: get the number of cpu c# 
Csharp :: how to find player gameobject in unity 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =