C# Basics

Welcome to the fundamental concepts of C#. This section will guide you through the essential building blocks of the C# programming language, enabling you to write simple yet powerful applications.

1. The Anatomy of a C# Program

A basic C# program consists of one or more files containing code that is organized into classes and methods. The entry point for most applications is the Main method.

A Simple "Hello, World!" Program


using System;

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

Explanation:

  • using System;: This directive imports the System namespace, which contains fundamental classes like Console.
  • public class HelloWorld: This declares a class named HelloWorld. In C#, all code resides within classes.
  • public static void Main(string[] args): This is the entry point of the program. public means it can be accessed from anywhere, static means it belongs to the class itself (not an instance), void means it doesn't return any value, and string[] args represents any command-line arguments passed to the program.
  • Console.WriteLine("Hello, World!");: This line uses the WriteLine method from the Console class to print the string "Hello, World!" to the console.

2. Comments

Comments are used to explain code and are ignored by the compiler. C# supports single-line and multi-line comments.

Using Comments


// This is a single-line comment

/*
   This is a
   multi-line comment
   explaining the next line.
*/
int myVariable = 10; // Variable declaration
                

3. Variables and Data Types

Variables are containers for storing data values. C# is a strongly-typed language, meaning you must declare the type of a variable before using it. Common data types include:

Variable Declaration and Initialization


int age = 30;
double price = 99.99;
bool isStudent = true;
string name = "Alice";
char initial = 'A';

Console.WriteLine($"Name: {name}, Age: {age}");
                

Tip: Type Inference with var

You can use the var keyword to let the compiler infer the data type of a variable. This can make your code more concise.


var message = "This is a string."; // Compiler infers string
var count = 100;              // Compiler infers int
                

4. Operators

Operators are special symbols that perform operations on variables and values. Key operators include:

Using Operators


int a = 10;
int b = 5;
int sum = a + b; // 15
bool isEqual = (a == b); // false
bool isGreater = (a > b); // true
Console.WriteLine($"Sum: {sum}, Is Equal: {isEqual}");
                

5. Control Flow Statements

Control flow statements allow you to control the order in which code is executed. Important control flow statements include:

if-else Statement and for Loop


int score = 75;

if (score >= 90) {
    Console.WriteLine("Grade: A");
} else if (score >= 80) {
    Console.WriteLine("Grade: B");
} else {
    Console.WriteLine("Grade: C");
}

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

6. Methods

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

Defining and Calling a Method


public class Calculator
{
    public static void Greet(string personName)
    {
        Console.WriteLine($"Hello, {personName}!");
    }

    public static int Add(int num1, int num2)
    {
        return num1 + num2;
    }

    public static void Main(string[] args)
    {
        Greet("Bob"); // Calling the Greet method
        int result = Add(10, 20); // Calling the Add method
        Console.WriteLine($"The sum is: {result}");
    }
}