Codectionary / Developer documentation / C#

Arithmetic & Assignment Operators

C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.

Syntax

+  -  *  /  %   +=  -=  ++  --

Examples

Basic Arithmetic

The core arithmetic operators, including integer division behavior.

int a = 10, b = 3;

Console.WriteLine(a + b);  // 13
Console.WriteLine(a - b);  // 7
Console.WriteLine(a * b);  // 30
Console.WriteLine(a / b);  // 3  (integer division truncates)
Console.WriteLine(a % b);  // 1  (remainder)

double result = 10.0 / 3;   // true division with a double
Console.WriteLine(result);  // 3.3333333333333335

Compound Assignment Operators

Shorthand operators that update a variable based on its current value.

int score = 10;
score += 5;   // score = score + 5  -> 15
score -= 3;   // 12
score *= 2;   // 24
score /= 4;   // 6

Console.WriteLine(score);

Increment and Decrement

The difference between prefix (++x) and postfix (x++) forms.

int x = 5;
Console.WriteLine(x++);  // prints 5, THEN increments (x is now 6)
Console.WriteLine(x);     // 6

int y = 5;
Console.WriteLine(++y);  // increments FIRST, then prints (y is now 6)
Console.WriteLine(y);     // 6

Best practices

  • Watch out for integer division truncating results - divide by a double or decimal literal when you need a precise fractional result
  • Use decimal rather than double for any calculation involving money, to avoid floating-point rounding errors
  • Be careful mixing prefix and postfix increment operators within the same complex expression - split them into separate statements for clarity
  • Use compound assignment operators (+=, -=, etc.) for conciseness when updating a variable based on itself

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and produce a bool. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting so the second operand is skipped once the result is already determined. For strings, == compares content by default (unlike some languages), since string overrides the equality operator.