HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C#VariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Dictionary
🔀 Condition & Loop
🧩 Function
C#›Operators

Operators

Math (+ - * / %), comparison (== != < >) and logical (&& || !) operators.

Simple enough for a 5-year-old to understand

Math operators

The division of two integers is an integer (7 / 2 = 3). At least one must be a double for decimals.

Example code
1
using System;
2
Console.WriteLine(7 + 3);
10
Output / Input10
3
Console.WriteLine(7 / 2);
3 (integer division)
Output / Input3 (integer division)
4
Console.WriteLine(7.0 / 2.0);
3.5 (decimal)
Output / Input3.5 (decimal)
5
Console.WriteLine(7 % 2);
1 (remainder)
Output / Input1 (remainder)
Output / Input
💡 Hint: % gives the remainder; very commonly used for even/odd checks.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of dividing the number 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

C# operators come from the C family and are familiar to those who know Java and C++. However, C# has its own unique, highly useful additions that significantly shorten everyday code.

The null-coalescing operator provides a default value if a value is null. The null-conditional access operator returns null instead of throwing an error when the object is null — eliminating the risk of NullReferenceException in chained access. These two operators are everywhere in modern C# code.

In comparison, C# differs from Java at one point: double equals for strings compares the CONTENT, not the reference. In C#, this operator is specially defined for the string class, so comparing text is safe.

What you will learn

  • Arithmetic and assignment operators
  • Comparison and logical operators
  • Null-coalescing and null-conditional access
  • The ternary conditional operator
  • Why string comparison is safe
  • Type checking with is and as

When to use?

Null-conditional access is used when drilling deep into nested objects: even if one step is null, instead of the chain crashing, it returns null. Combined with null-coalescing, you provide a safe default in a single line.

You can perform a type check with the is operator and assign it to a variable in the same expression; this is both shorter and safer than the old-school way of checking and then casting.

Common mistakes

Wrong

int i = 5;
int result = i++ + i;
Console.WriteLine(result);

Postfix increment increases the value after using it; the expression evaluates as 5 plus 6 and gives 11. Incrementing and reading a variable in the same line harms readability and invites bugs.

Correct

int i = 5;
i++;
int result = i + i;
Console.WriteLine(result);

Wrong

string? name = null;
Console.WriteLine(name.ToUpper());

Calling a method on null throws a "NullReferenceException". Using null-conditional access and null-coalescing together provides a safe default and also resolves the compiler warning.

Correct

string? name = null;
Console.WriteLine(name?.ToUpper() ?? "");

Wrong

object value = "text";
int number = (int) value;

Direct casting throws an "InvalidCastException" if types don't match. For safe conversion, the as operator or pattern-matching is should be used; both allow checking instead of throwing an error.

Correct

object value = "text";
if (value is int number) {
    Console.WriteLine(number);
}

Frequently asked questions

Can I use double equals to compare strings in C#?

Yes. C# specially defines this operator for the string class and compares the content. Those coming from Java get nervous here, but it is safe in C#. For case-insensitive comparison, the option to compare with string.Equals is provided.

What is the null-coalescing assignment?

It is a compound operator that assigns a value to a variable if it is null, and leaves it alone if it is not. It is heavily used in the lazy initialization pattern: a single line is enough to create an object only on the first access.

What is the difference between is and as?

is performs a type check and returns a boolean; with pattern matching, it also defines a variable in the same expression. as tries to cast and returns null if it fails. In modern C#, pattern-matching is is more readable in almost every case.

Summary

C# operators come from the C family, but null-coalescing and null-conditional access are uniquely powerful additions. Double equals compares content for strings, and pattern-matching is is the safest way for type casting.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code