The short answer: symbols that act on values
C# operators are symbols such as +, ==, && and ??. Each one tells the compiler to do something with one or more values, which are called operands. C# is a different language from C++, but most of its operators look and behave much like the ones C++ programmers already use. The main differences are a few additions, such as ??, and some things C# leaves out.
Arithmetic, comparison and logical operators
| Group | C# operators | Compared with C++ |
|---|---|---|
| Arithmetic | + - * / % |
Same symbols. Integer division truncates in both. |
| Comparison | == != < > <= >= |
Same symbols. Both return a true or false value. |
| Logical | && || ! |
Same symbols. They work on true or false values. |
int a = 7;
int b = 2;
Console.WriteLine(a / b); // prints 3, because both operands are int
Console.WriteLine(a % b); // prints 1, the remainder
Console.WriteLine(a > b && b > 0); // prints True
Assignment and increment operators
= assigns a value. The compound forms +=, -=, *=, /= and %= combine an arithmetic operation with assignment, so a += 3 means a = a + 3. ++ adds one and -- subtracts one. These work the same way they do in C++.
Bitwise and shift operators
C# uses &, |, ^, ~, << and >> for bit operations, the same symbols as C++. They work on integer types. &, | and ^ also work on bool values, where they combine the two values without skipping the second one.
Member access and the dot
Like C++, C# uses the dot (.) to reach an object’s members, such as a property or a method. The dot operator in C++ works the same way for structs and classes. In C#, the length of a string is a property:
string word = "hello";
Console.WriteLine(word.Length); // prints 5
Outside unsafe code, C# uses the dot for every object, so you do not need an arrow operator.
C#-only operators: ??, ?., is, as and =>
??(null-coalescing) returns the value on its left unless that value is null. In that case it returns the value on its right.?.(null-conditional) reads a member only if the object is not null. If the object is null, the whole expression gives null instead of throwing an exception.ischecks whether a value has a given type. It can also declare a variable for that type.astries to treat a value as a type and gives null if the value is not of that type.=>is the lambda operator, which separates a lambda’s parameters from its body.
string name = null;
Console.WriteLine(name ?? "unknown"); // prints: unknown
object item = "hello";
if (item is string text)
{
Console.WriteLine(text.Length); // prints: 5
}
Func<int, int> doubled = x => x * 2;
Console.WriteLine(doubled(4)); // prints: 8
Overloading operators in C#
A C# class or struct can define what some operators do, using the operator keyword. Only a fixed list of operators can be overloaded, such as +, -, == and ++. The member access dot, =>, ?? and the conditional operator ?: are not on that list. C++ also lets you overload many operators, with its own fixed list, which is covered in operator overloading in C++.
Where C# differs from C++
- No arrow for ordinary objects. Outside unsafe code, the dot reaches every member, whether you hold the object directly or through a reference, so there is no
->. Pointer syntax is only available in unsafe code. - No pointer arithmetic in safe code. You cannot add to a pointer or subtract from one outside unsafe code.
- A narrow use of the double colon. C# uses
::for namespace aliases andglobal::. It does not use it to look up class members the way C++ does.
Key points to remember
- Most C# operators for arithmetic, comparison, logic, bits and assignment match the ones in C++.
- C# uses the dot to reach members, as C++ does.
- C# adds
??,?.,is,asand=>. - Only a fixed list of operators can be overloaded, and the dot is not on it.