Lesson 3 +10 XP

C# Syntax

C# Syntax

C# syntax is close to languages like C, C++, and Java. Here's a classic first program:

using System;

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

Every statement ends with a semicolon

Like C and Java, each statement in C# ends with a semicolon (;):

Console.WriteLine("Hi");
Console.WriteLine("There");

Code blocks use curly braces

Groups of code are wrapped in curly braces { }:

{
  // code lives here
}

The parts of the program

  • using System; - imports the System namespace so you can use Console.
  • namespace HelloWorld - a container that organizes code.
  • class Program - C# programs are built from classes.
  • static void Main(string[] args) - the entry point: where the program starts. C# programs always start by running Main.

Case sensitivity

C# is case-sensitive: Main and main are different. Keywords like class and void must be lowercase.

Whitespace is ignored

Extra spaces, tabs, and blank lines don't matter to the compiler - they just make code readable.

TL;DR

  • Statements end with a semicolon ;.
  • Code blocks use curly braces { }.
  • The entry point is static void Main(string[] args).
  • C# is case-sensitive.