Learn C# Programming

Master C# programming from basics to advanced concepts with our comprehensive tutorial series. Perfect for beginners and experienced developers.

C# Operators

Operators are symbols that perform operations on variables and values. C# provides various types of operators for different operations.

1. Arithmetic Operators

Used to perform mathematical operations:

int a = 10, b = 3;

int addition = a + b;        // 13 - Addition
int subtraction = a - b;     // 7  - Subtraction
int multiplication = a * b;  // 30 - Multiplication
int division = a / b;        // 3  - Division (integer division)
int modulus = a % b;         // 1  - Modulus (remainder)

// Increment and Decrement
int x = 5;
x++;    // Post-increment: x becomes 6
++x;    // Pre-increment: x becomes 7
x--;    // Post-decrement: x becomes 6
--x;    // Pre-decrement: x becomes 5

2. Assignment Operators

Used to assign values to variables:

int x = 10;

x = 5;      // Simple assignment
x += 3;     // x = x + 3  (x becomes 8)
x -= 2;     // x = x - 2  (x becomes 6)
x *= 4;     // x = x * 4  (x becomes 24)
x /= 3;     // x = x / 3  (x becomes 8)
x %= 5;     // x = x % 5  (x becomes 3)

3. Comparison Operators

Used to compare values and return boolean results:

int a = 10, b = 5;

bool equal = (a == b);          // false - Equal to
bool notEqual = (a != b);       // true  - Not equal to
bool greater = (a > b);         // true  - Greater than
bool less = (a < b);            // false - Less than
bool greaterEqual = (a >= b);   // true  - Greater than or equal
bool lessEqual = (a <= b);      // false - Less than or equal

4. Logical Operators

Used to combine boolean expressions:

bool x = true, y = false;

bool andResult = x && y;    // false - Logical AND
bool orResult = x || y;     // true  - Logical OR
bool notResult = !x;        // false - Logical NOT

// Short-circuit evaluation
bool result = (x && SomeMethod()); // SomeMethod() called only if x is true
bool result2 = (y || SomeMethod()); // SomeMethod() called only if y is false

5. Bitwise Operators

Used to perform operations on individual bits:

int a = 5;  // Binary: 101
int b = 3;  // Binary: 011

int bitwiseAnd = a & b;     // 1   (001) - Bitwise AND
int bitwiseOr = a | b;      // 7   (111) - Bitwise OR
int bitwiseXor = a ^ b;     // 6   (110) - Bitwise XOR
int bitwiseNot = ~a;        // -6        - Bitwise NOT
int leftShift = a << 1;     // 10  (1010) - Left shift
int rightShift = a >> 1;    // 2   (10)   - Right shift

6. Conditional (Ternary) Operator

A shorthand for if-else statements:

int age = 18;
string status = (age >= 18) ? "Adult" : "Minor";

// Equivalent to:
string status2;
if (age >= 18)
    status2 = "Adult";
else
    status2 = "Minor";

// Nested ternary operators
int score = 85;
string grade = (score >= 90) ? "A" : 
               (score >= 80) ? "B" : 
               (score >= 70) ? "C" : "F";

7. Null-Coalescing Operators

Used to handle null values:

string name = null;
string displayName = name ?? "Unknown";  // "Unknown" if name is null

// Null-coalescing assignment (C# 8.0+)
name ??= "Default Name";  // Assign "Default Name" if name is null

// Null-conditional operators
string text = name?.ToUpper();  // Call ToUpper() only if name is not null
int? length = name?.Length;     // Get length only if name is not null

8. Type Testing Operators

Used to check types at runtime:

object obj = "Hello";

// is operator
if (obj is string)
{
    Console.WriteLine("obj is a string");
}

// as operator
string str = obj as string;  // Returns null if conversion fails
if (str != null)
{
    Console.WriteLine($"String value: {str}");
}

// typeof operator
Type stringType = typeof(string);
Console.WriteLine($"Type name: {stringType.Name}");

9. Operator Precedence

Order of operations (highest to lowest precedence):

int result = 2 + 3 * 4;        // 14, not 20 (multiplication first)
int result2 = (2 + 3) * 4;     // 20 (parentheses override precedence)

// Precedence order:
// 1. () [] . -> ++ -- (postfix)
// 2. ++ -- + - ! ~ (prefix)
// 3. * / %
// 4. + -
// 5. << >>
// 6. < <= > >= is as
// 7. == !=
// 8. &
// 9. ^
// 10. |
// 11. &&
// 12. ||
// 13. ?:
// 14. = += -= *= /= %= etc.

💡 Best Practices

  • Use parentheses to make operator precedence clear
  • Prefer == over Equals() for value comparisons
  • Use && and || for short-circuit evaluation
  • Use null-conditional operators to avoid null reference exceptions
  • Be careful with integer division - use double for precise results
  • Use compound assignment operators (+=, -=) for cleaner code

Frequently Asked Questions

C# is a modern, object-oriented programming language developed by Microsoft. It's part of the .NET framework and is used for developing various applications including web applications, desktop software, mobile apps, and games.

No! You can start learning C# immediately using our online compiler. For advanced development, you can install the .NET SDK and Visual Studio or VS Code on your computer.

Yes! C# has a clean syntax and strong type system that makes it beginner-friendly. Our tutorial starts from the basics and gradually progresses to advanced topics.

C# is versatile and can be used to build web applications (ASP.NET), desktop applications (WPF, WinForms), mobile apps (Xamarin), games (Unity), APIs, microservices, and much more.

The time varies based on your programming background. Complete beginners can learn the basics in 2-3 months with consistent practice. Our structured tutorial helps you progress systematically.

Master C# Programming with Our Comprehensive Tutorial

Our C# programming tutorial is designed to take you from a complete beginner to an advanced developer. Whether you're looking to build web applications, desktop software, or mobile apps, C# provides the foundation you need for modern software development.

Start with the basics like variables, data types, and control structures, then progress to advanced topics including object-oriented programming, LINQ, async programming, and more. Each lesson includes practical examples and exercises to reinforce your learning.

Join thousands of developers who have learned C# programming through our step-by-step tutorials. Begin your journey today and unlock the power of .NET development.