> ## 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.

# C# Remainder

The `%` operator in C# is the remainder operator. It computes the remainder resulting from the division of its left-hand operand (dividend) by its right-hand operand (divisor).

```csharp theme={"dark"}
var result = dividend % divisor;
```

## Type Support and Numeric Promotion

The `%` operator is predefined for the following standard numeric types:

* **Integer types:** `int`, `uint`, `long`, `ulong`.
* **Floating-point types:** `float`, `double`.
* **High-precision decimal:** `decimal`.

When the `%` operator is applied to smaller integral types (`sbyte`, `byte`, `short`, `ushort`), C# applies implicit numeric promotion. The operands are automatically converted to `int` before the operation is evaluated, and the resulting remainder is of type `int`.

```csharp theme={"dark"}
byte a = 5;
byte b = 3;
var result = a % b; // result is of type int, not byte
```

## Sign Determination Rules

In C#, the sign of a non-zero remainder is strictly determined by the left-hand operand (the dividend). The sign of the right-hand operand (the divisor) has no effect on the sign of the result.

```csharp theme={"dark"}
Console.WriteLine( 5 %  3);  // Output:  2
Console.WriteLine(-5 %  3);  // Output: -2
Console.WriteLine( 5 % -3);  // Output:  2
Console.WriteLine(-5 % -3);  // Output: -2
```

## Floating-Point Mechanics

Unlike some C-family languages, C# natively supports the `%` operator for floating-point types (`float` and `double`). For finite values, the remainder is conceptually calculated by truncating the intermediate quotient towards zero.

Due to standard IEEE 754 floating-point precision mechanics, using `float` or `double` can result in minor precision artifacts. To achieve exact base-10 fractional remainders, the `decimal` type must be used:

```csharp theme={"dark"}
Console.WriteLine(5.5m % 2.2m); // Output: 1.1 (Exact decimal evaluation)
Console.WriteLine(5.5 % 2.2);   // Output: 1.0999999999999996 (IEEE 754 double precision)
```

When evaluating floating-point operands, the `%` operator adheres to specific rules regarding `NaN` (Not a Number) and infinity:

* If either operand is `NaN`, the result is `NaN`.
* If the dividend is infinity, the result is `NaN`.
* If the divisor is zero, the result is `NaN` (no exception is thrown).
* If the dividend is finite and the divisor is infinity, the result is the dividend.

```csharp theme={"dark"}
Console.WriteLine(5.0 % double.PositiveInfinity); // Output: 5
Console.WriteLine(double.PositiveInfinity % 5.0); // Output: NaN
Console.WriteLine(5.0 % 0.0);                     // Output: NaN
```

## Exceptions and Edge Cases

The behavior of the `%` operator under exceptional conditions depends entirely on the numeric types involved:

* **Integer Division by Zero:** Evaluating `x % 0` where `x` is an integer type throws a `DivideByZeroException`.
* **Decimal Division by Zero:** Evaluating `x % 0m` throws a `DivideByZeroException`.
* **Integer Overflow:** Evaluating `int.MinValue % -1` (or `long.MinValue % -1L`) throws an `OverflowException`. This occurs because the underlying division operation (`int.MinValue / -1`) results in a value that exceeds the maximum bounds of the signed integer type.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
