C# Operators

In C#, operators are symbols used to perform operations on variables and values. They help manipulate data and perform calculations or comparisons.

Operators are a fundamental part of expressions and decision-making in C# programs.

Types of Operators in C#

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Unary Operators

1. Arithmetic Operators

Used to perform mathematical operations.

Operator Description Example
+ Addition int c = a + b;
- Subtraction int c = a - b;
* Multiplication int c = a * b;
/ Division int c = a / b;
% Modulus (remainder) int c = a % b;

2. Relational Operators

Used to compare two values and return a boolean result.

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

3. Logical Operators

Used to combine multiple conditions.

Operator Description Example
&& Logical AND a > 0 && b > 0
|| Logical OR a > 0 || b > 0
! Logical NOT !isActive

4. Assignment Operators

Used to assign values to variables.

Operator Description Example
= Assign a = 10;
+= Add and assign a += 5;
-= Subtract and assign a -= 5;

5. Unary Operators

Operate on a single operand.

  • ++ Increment
  • -- Decrement

int x = 5;
x++;
--x;
    

Key Points

  • Operators perform actions on data
  • Logical operators return boolean values
  • Assignment operators update variable values
  • Correct operator usage avoids logical errors

Conclusion

Operators are essential for calculations, comparisons, and decision-making in C#. Mastering operators helps in writing efficient and readable code.

C# Operators Quiz

Q1. Which operator is used for comparison?



Q2. Which operator means logical AND?



Q3. What does ++ do?


C# Introduction C# Variables & Keywords C# Data Types C# If Else Statement C# Loops C# Comments C# Type Casting C# User Input C# Operators C# Math C# String C# OOP Concepts C# Classes and Objects C# Multiple Classes and Objects C# Class Members C# Constructors C# Access Modifiers