Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity c# class addition syntax

//Operator Overloads
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 :: InvalidOperationException: Calling Scene Raisefrom assembly reloading callbacks are not supported. 
Csharp :: how to open any file on button click in winforms 
Csharp :: socket would block error c# 
Csharp :: c# get property using string 
Csharp :: remove last character from string c# 
Csharp :: c# convert utc to est 
Csharp :: animations for pause menu 
Csharp :: system.linq.iorderedenumerable`2[system.char,system.char] çözümü 
Csharp :: c# declare inline string array 
Csharp :: c# regex number only 
Csharp :: c# unity 2d play video 
Csharp :: gravity script unity 
Csharp :: unity instantiate 
Csharp :: c# byte array to string 
Csharp :: c# string newline 
Csharp :: how to store user input into list c# 
Csharp :: c# request run as administrator 
Csharp :: c# separate string by comma 
Csharp :: convert string to short c# 
Csharp :: .net core add header to soap request 
Csharp :: c# calculate difference between two dates in days 
Csharp :: delete file from FTP c# 
Csharp :: c# choose first n elements from list 
Csharp :: difference between executescalar and executenonquery and executereader 
Csharp :: c# start file 
Csharp :: merge point 
Csharp :: how to make a enum list in c# 
Csharp :: c# countdown timer menutes 
Csharp :: c# check if string is path or file 
Csharp :: how to find a gameobject in unity 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =