Search⌘ K
AI Features

Assignment and Logical Operators

Explore how to use assignment operators combined with arithmetic operations and apply logical operators like AND, OR, XOR, and their conditional variants in C#. Understand the differences between bitwise and short-circuiting logical operators and see practical examples using Boolean values to write concise and efficient code.

Operators like =, +, -, *, and / can be combined with an assignment to shorthand expressions. Logical operators like &, |, and ^ act on Boolean values to perform logic operations. Understanding these operators can help us write more efficient C# code.

Assignment operators

We have been using the most common assignment operator, =. To make our code more concise, we can combine the assignment operator with other operators, like arithmetic operators, as shown in the following code:

C#
int p = 6;
p += 3; // equivalent to p = p + 3;
p -= 3; // equivalent to p = p - 3;
p *= 3; // equivalent to p = p * 3;
p /= 3; // equivalent to p = p / 3;

Exploring logical operators

...