Operators
Prerequisites: Type casting & conversion
C# supports the usual arithmetic (+ - * / % ++ --), comparison
(== != > < >= <=), and logical (&& || !) operators — and, for
string, == compares content, not references, unlike Java.
int a = 7, b = 2;
Console.WriteLine(a / b); // 3 — integer division truncates
Console.WriteLine((double) a / b); // 3.5 — cast one side first
string x = "hi";
string y = "hi";
Console.WriteLine(x == y); // true — compares content, as expected
Common mistakes
- Forgetting integer division truncates:
7 / 2is3, not3.5.