Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The / operator is a binary arithmetic operator that computes the quotient of its left-hand operand (the dividend) divided by its right-hand operand (the divisor). The execution behavior, return type, and exception handling of the operation are strictly determined by the numeric types of the operands evaluated at compile time.
var quotient = dividend / divisor;

Type Resolution and Execution Behavior

The C# compiler applies implicit numeric promotion to the operands before evaluating the division. Depending on the resolved types, the operator’s behavior falls into three distinct categories:

1. Integer Division

When both operands are of integral types (int, uint, long, ulong), the operator performs integer division.
  • Result: The fractional part of the quotient is discarded (truncated towards zero).
  • Exceptions:
    • Throws a System.DivideByZeroException if the right-hand operand evaluates to 0 at runtime.
    • Throws a System.OverflowException if dividing int.MinValue or long.MinValue by -1. This occurs regardless of the checked or unchecked context because the mathematically positive result exceeds the maximum representable value of the signed 32-bit or 64-bit integer type.
int a = 13 / 5;  // Evaluates to 2
int b = -13 / 5; // Evaluates to -2

int zero = 0;
int c = 1 / zero; // Throws DivideByZeroException at runtime

int minusOne = -1;
int d = int.MinValue / minusOne; // Throws OverflowException at runtime

2. Floating-Point Division

When at least one operand is a floating-point type (float or double), the operator performs floating-point division in compliance with the IEEE 754 standard.
  • Result: A floating-point quotient retaining fractional precision.
  • Exceptions: Never throws an exception. Division by zero yields specific constant values:
    • PositiveInfinity: Non-zero positive dividend divided by 0.0.
    • NegativeInfinity: Non-zero negative dividend divided by 0.0.
    • NaN (Not a Number): 0.0 divided by 0.0.
double x = 13.0 / 5;   // Evaluates to 2.6
double y = 1.0 / 0.0;  // Evaluates to double.PositiveInfinity
double z = 0.0 / 0.0;  // Evaluates to double.NaN

3. Decimal Division

When at least one operand is of type decimal (and the other is not a floating-point type), the operator performs high-precision base-10 division.
  • Result: A decimal quotient. The scale of the result is determined by the scales of the operands and the precision required to represent the quotient.
  • Exceptions:
    • Throws a System.DivideByZeroException if the right-hand operand evaluates to 0m at runtime.
    • Throws a System.OverflowException if the resulting quotient is too large to be represented within the decimal type’s limits.
decimal d1 = 13m / 5m; // Evaluates to 2.6m

decimal zero = 0m;
decimal d2 = 1m / zero; // Throws DivideByZeroException at runtime

decimal fractional = 0.1m;
decimal d3 = decimal.MaxValue / fractional; // Throws OverflowException at runtime

Operator Overloading

The / operator can be overloaded for user-defined types (classes and structs) using the operator keyword. The method must be declared as public static.
public readonly struct Vector2
{
    public double X { get; }
    public double Y { get; }

    public Vector2(double x, double y)
    {
        X = x;
        Y = y;
    }

    public static Vector2 operator /(Vector2 vector, double scalar)
    {
        return new Vector2(vector.X / scalar, vector.Y / scalar);
    }
}

Compound Assignment

The / operator serves as the foundation for the /= compound assignment operator. This operator divides the left-hand variable by the right-hand operand and assigns the resulting quotient back to the left-hand variable. When a custom type overloads the / operator, the /= operator is implicitly overloaded.
int value = 10;
value /= 2; // Semantically equivalent to: value = value / 2;
Master C# with Deep Grasping Methodology!Learn More