</>
Skip to content
C# lessons (4/34)

C# — Syntax

Basic program structure

using System;

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

Top-level statements (C# 9+)

Simplified entry point:

Console.WriteLine("Hello, World!");
Console.WriteLine("No class or Main needed!");

// args is available directly
if (args.Length > 0)
{
    Console.WriteLine($"First arg: {args[0]}");
}

Namespaces

Organize code into logical groups:

namespace MyProject.Models
{
    public class User
    {
        public string Name { get; set; }
    }
}

// Or use file-scoped namespace (C# 10)
namespace MyProject.Services;

public class EmailService
{
    public void Send(string to, string body) { }
}

Using directives

Import namespaces:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// Alias a type
using Point = System.Drawing.Point;

// Global using (C# 10)
global using System.Text;

Classes and methods

using System;

namespace MyApp
{
    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Multiply(int a, int b) => a * b;

        public static void Main()
        {
            var calc = new Calculator();
            Console.WriteLine(calc.Add(3, 4));       // 7
            Console.WriteLine(calc.Multiply(3, 4));  // 12
        }
    }
}

Code blocks and scope

using System;

class Program
{
    static void Main()
    {
        int x = 10;

        if (x > 5)
        {
            int y = 20;
            Console.WriteLine($"x={x}, y={y}");
        }

        // Console.WriteLine(y); // Error: y is out of scope

        // Block-scoped for loop
        for (int i = 0; i < 5; i++)
        {
            Console.Write($"{i} ");
        }
        Console.WriteLine();
    }
}

Semicolons and braces

C# uses:

  • Semicolons to end statements
  • Braces {} to define blocks
  • No significant whitespace (unlike Python)
// Statement ending
int x = 10;
Console.WriteLine(x);

// Single-line if (no braces needed)
if (x > 5) Console.WriteLine("big");

// Always use braces for clarity
if (x > 5)
{
    Console.WriteLine("big");
}

var keyword

Let the compiler deduce the type:

var name = "Alice";    // string
var age = 30;          // int
var pi = 3.14;         // double
var isActive = true;   // bool
var numbers = new[] { 1, 2, 3 }; // int[]

// Explicit type is still valid
string explicitName = "Bob";
int explicitAge = 25;

Comments

// Single-line comment

/* Multi-line
   comment */

/// <summary>
/// XML documentation comment.
/// Used for generating API docs.
/// </summary>
/// <param name="x">The input value</param>
/// <returns>The squared value</returns>
public static int Square(int x) => x * x;

Mini Practice

Write C# code that:

  1. Creates a basic program with a class and Main method
  2. Uses top-level statements
  3. Declares variables using var and explicit types
  4. Writes XML documentation comments for a method

Up Next

In the next lesson, you'll learn about Output — printing to the console in C#.

Related Topics

Frequently Asked Questions about Syntax

What is Syntax in C#?

Syntax is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Syntax?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.

Why is Syntax important in C#?

Syntax is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.