C# ยท Chapter 4 of 46

C# Syntax

C# syntax defines how programs are structured: statements end with a semicolon, and blocks of code are grouped using curly braces. C# is case-sensitive, so `Total` and `total` are different identifiers.

C# code is organized into namespaces, classes, and methods. Understanding this structure early makes it much easier to read and write larger programs later.

Syntax
class Program {
  static void Main() {
    statement1;
    statement2;
  }
}

Statements and blocks

Each instruction (a statement) ends with a semicolon. Related statements are grouped into a block using curly braces, such as the body of a method or an if statement.

Identifiers and case sensitivity

Names for variables, methods and classes can contain letters, digits and underscores but cannot start with a digit. C# distinguishes uppercase from lowercase letters.

Example 1 (csharp)
using System;

class Program {
  static void Main() {
    int age = 25;
    Console.WriteLine("Age: " + age);
  }
}
Output
Age: 25

A block is enclosed in braces, and each statement ends with a semicolon.

Example 2 (csharp)
using System;

class Program {
  static void Main() {
    int Age = 1;
    int age = 2;
    Console.WriteLine(Age + " " + age);
  }
}
Output
1 2

Age and age are treated as two different variables because C# is case-sensitive.

Key points

  • Statements end with a semicolon.
  • Curly braces { } group statements into blocks.
  • C# is case-sensitive.
  • Code is organized into namespaces, classes, and methods.
๐Ÿ’ก Note: Consistent indentation and naming conventions (like PascalCase for classes) make C# code much easier to read.

๐Ÿ“ Quick Quiz

1. What character ends a C# statement?

2. Is C# case-sensitive?

3. What symbol groups statements into a block?