C# Language Fundamentals

This section provides a deep dive into the core language constructs of C#, enabling you to build robust and efficient .NET applications.

Variables and Data Types

Understanding data types is fundamental to programming. C# offers a rich set of built-in value types and reference types.

Value Types

Value types directly contain their data. Examples include:

Reference Types

Reference types store a reference to the object's location in memory. Examples include:

int age = 30;
string name = "Alice";
bool isActive = true;
double price = 99.99;

Operators

Operators perform operations on operands. C# supports various categories of operators:

int x = 10;
int y = 5;
int sum = x + y; // sum is 15
bool isGreater = x > y; // isGreater is true

Control Flow Statements

Control flow statements dictate the order in which code is executed.

Conditional Statements

if (temperature > 30) {
    Console.WriteLine("It's hot!");
} else if (temperature < 10) {
    Console.WriteLine("It's cold!");
} else {
    Console.WriteLine("It's moderate.");
}

Looping Statements

for (int i = 0; i < 5; i++) {
    Console.WriteLine($"Iteration: {i}");
}

string[] fruits = {"Apple", "Banana", "Cherry"};
foreach (string fruit in fruits) {
    Console.WriteLine(fruit);
}

Methods

Methods are blocks of code that perform a specific task. They help in modularizing code and promoting reusability.

public int Add(int a, int b) {
    return a + b;
}

int result = Add(5, 3); // result is 8

Key Takeaway

Mastering these fundamental building blocks is crucial for any C# developer. They form the basis of more complex programming patterns and application development.