C# Fundamentals

Welcome to the foundational concepts of C#. This section provides an in-depth look at the core elements that make C# a powerful and versatile object-oriented programming language.

Basic Syntax and Structure

C# syntax is derived from C and C++, featuring curly braces to define code blocks and semicolons to terminate statements. Understanding the fundamental structure of a C# program is crucial.


using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}
            

Data Types

C# is a strongly typed language, meaning all variables must have a declared type. This helps catch errors at compile time. Common data types include:

For a comprehensive list, see the MSDN Data Types Reference.

Variables and Constants

Variables are named memory locations that can store data which may change. Constants are similar but their values cannot be modified after initialization.


int score = 100;
const double PI = 3.14159;
string userName = "Alice";
            

Operators

C# supports a wide range of operators for performing operations:

Operator precedence dictates the order in which operations are performed. You can use parentheses to override this order.

Control Flow Statements

Control flow statements allow you to direct the execution path of your program based on certain conditions.

Conditional Statements

if, else if, else, and switch statements are used for decision-making.


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

switch (dayOfWeek)
{
    case 0:
        Console.WriteLine("Sunday");
        break;
    case 1:
        Console.WriteLine("Monday");
        break;
    default:
        Console.WriteLine("Another day.");
        break;
}
            

Looping Statements

for, while, do-while, and foreach loops are used to execute a block of code repeatedly.


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

int count = 0;
while (count < 3)
{
    Console.WriteLine("While loop: " + count);
    count++;
}

string[] names = { "Bob", "Charlie" };
foreach (string name in names)
{
    Console.WriteLine("Hello, " + name);
}
            

Methods (Functions)

Methods are blocks of code that perform specific tasks. They help in organizing code and promoting reusability.


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

// Calling the method
int sum = Add(5, 3);
            

Note: Methods can have return types (e.g., int, string) or be void if they don't return a value.

Introduction to Object-Oriented Programming (OOP)

C# is fundamentally an object-oriented language. OOP concepts like classes, objects, encapsulation, inheritance, and polymorphism are central to its design.

For more detail, proceed to the Classes and Objects section.

Key Takeaway: Mastering these fundamental C# concepts is the first step towards building robust and scalable applications.