C# operators are special symbols used to perform operations on variables and values in a C# program. Understanding C# operators is one of the most important steps in mastering C# programming, .NET development, and writing efficient business logic in applications.
Operators in C# are used in expressions to manipulate data, compare values, perform mathematical calculations, control program flow, and work with bits and memory. Whether you are preparing for C# interview questions, building ASP.NET applications, or working on console applications, knowing all C# operators is essential.
Arithmetic operators in C# are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 3;
Console.WriteLine("Addition: " + (a + b));
Console.WriteLine("Subtraction: " + (a - b));
Console.WriteLine("Multiplication: " + (a * b));
Console.WriteLine("Division: " + (a / b));
Console.WriteLine("Modulus: " + (a % b));
}
}
These operators are widely used in C# console applications, financial software, gaming applications, and scientific computing.
Relational operators are used to compare two values. They return a Boolean value (true or false).
int x = 20;
int y = 15;
Console.WriteLine(x == y);
Console.WriteLine(x != y);
Console.WriteLine(x > y);
Console.WriteLine(x < y);
Console.WriteLine(x >= y);
Console.WriteLine(x <= y);
Relational operators are essential in conditional statements in C# like if, while, and for loops.
Logical operators are used to combine multiple conditions in C#.
int age = 25;
bool hasLicense = true;
if (age >= 18 && hasLicense)
{
Console.WriteLine("Eligible to drive");
}
Logical operators are very important in C# decision making and form validation in ASP.NET Core applications.
Assignment operators are used to assign values to variables.
int number = 10;
number += 5;
number *= 2;
Console.WriteLine(number);
Compound assignment improves readability and is frequently asked in C# interview questions for freshers.
These operators increase or decrease a variableβs value by 1.
int count = 5;
count++;
Console.WriteLine(count);
count--;
Console.WriteLine(count);
They are commonly used in loops in C#.
Bitwise operators operate at the binary level.
int a = 5;
int b = 3;
Console.WriteLine(a & b);
Console.WriteLine(a | b);
Console.WriteLine(a ^ b);
Console.WriteLine(~a);
Console.WriteLine(a << 1);
Console.WriteLine(a >> 1);
Bitwise operators are important in low-level programming and performance optimization in C#.
The ternary operator is a shorthand for if-else statements.
condition ? expression1 : expression2;
int age = 18;
string result = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(result);
It makes code concise and readable in C# real-world applications.
The null-coalescing operator returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
string name = null;
string displayName = name ?? "Guest";
Console.WriteLine(displayName);
This operator is widely used in modern C# programming and .NET Core applications.
object obj = "Hello";
if (obj is string)
{
Console.WriteLine("It is a string");
}
object obj = "World";
string str = obj as string;
Console.WriteLine(str);
These operators are important in object-oriented programming in C#.
The lambda operator is used to define anonymous functions.
Func square = x => x * x;
Console.WriteLine(square(5));
Lambda expressions are heavily used in LINQ in C#.
int[] numbers = { 10, 20, 30, 40 };
Console.WriteLine(numbers[^1]);
int[] numbers = { 10, 20, 30, 40, 50 };
int[] slice = numbers[1..4];
These are modern features introduced in recent versions of C#.
Operator precedence determines which operator executes first in an expression.
Order (High to Low):
Understanding precedence prevents logical errors in C# coding.
C# provides a rich set of operators that make programming powerful and expressive. From arithmetic and relational operators to advanced lambda and range operators, each plays a crucial role in C# development.
Mastering C# operators with examples improves your problem-solving ability, prepares you for C# technical interviews, and enhances your expertise in .NET programming.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved