Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Lab: the C# operators

Operators are the small symbols that do the work: add, compare, combine. There are fewer than you think, and they group neatly.

Arithmetic

Plus, minus, star and slash do what you expect. The percent sign gives the remainder, which is the usual way to test whether a number is even.

Comparison

Equal to, not equal to, greater than and less than each produce a boolean. Two equals signs compare; one equals sign assigns. Confusing the two is the single most common beginner mistake.

C#
int a = 10;
int b = 3;

int remainder = a % b;      // 1
bool isBigger = a > b;    // true
bool bothTrue = isBigger && (b > 0);

Logical

Two ampersands mean both conditions must hold. Two vertical bars mean either one may hold. A single exclamation mark reverses a boolean.

Exercise. Write a small page that stores two numbers, then prints the sum, the difference and whether the first is larger than the second.

Operators only become useful once you can group statements, so continue to blocks of code and the semicolon.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.