Basic Syntax
Program Structure
A classic C# program consists of using directives, a namespace, a class, and a Main method.
- using directive: Imports a namespace so you don't have to write fully qualified paths
- namespace: A logical container for organizing code and preventing naming conflicts
- class: C# is object-oriented; all code must reside inside a class
- Main method: The program's entry point where execution begins
Example
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Hello, World!
Statements and Semicolons
Every statement in C# must end with a semicolon ;. The semicolon is a statement terminator that tells the compiler where one instruction ends. A missing semicolon will cause a compilation error.
Example
using System;
namespace StatementDemo
{
class Program
{
static void Main()
{
int x = 10;
int y = 20;
int sum = x + y;
Console.WriteLine(sum);
}
}
}
30
Code Blocks and Braces
A code block is wrapped in braces { }, grouping multiple statements into a single logical unit. Namespaces, classes, methods, conditionals, and loops all require code blocks. Braces can be nested.
namespace Outer
{
namespace Inner
{
class MyClass
{
void MyMethod()
{
if (true)
{
Console.WriteLine("nested block");
}
}
}
}
}
Indentation and Code Style
Good indentation makes code easier to read. The C# community recommends 4 spaces for indentation. There are two common brace styles:
- Allman style: Opening brace on its own line (common in C#)
- K&R style: Opening brace at the end of the previous line
class StyleA
{
void Method()
{
Console.WriteLine("Allman style");
}
}
class StyleB {
void Method() {
Console.WriteLine("K&R style");
}
}
Tip: Visual Studio uses Allman style by default. What matters most is consistency within a project.
Console Output
Console.Write and Console.WriteLine are used to output text to the console:
- Console.Write: Outputs text without a trailing newline
- Console.WriteLine: Outputs text followed by a newline
Example
using System;
namespace ConsoleDemo
{
class Program
{
static void Main()
{
Console.Write("Hello");
Console.Write(" ");
Console.WriteLine("World");
Console.WriteLine("Second line");
}
}
}
Hello World
Second line
Escape Characters
Certain characters inside strings must be escaped with a backslash \ to be represented correctly:
| Escape Sequence | Meaning |
|---|---|
\n |
Newline |
\t |
Tab |
\\ |
Backslash |
\" |
Double quote |
\' |
Single quote |
Example
using System;
namespace EscapeDemo
{
class Program
{
static void Main()
{
Console.WriteLine("Line1\nLine2");
Console.WriteLine("Name\tAge");
Console.WriteLine("Path: C:\\Users\\Doc");
Console.WriteLine("He said \"Hello\"");
}
}
}
Line1
Line2
Name Age
Path: C:\Users\Doc
He said "Hello"
Warning: If a string contains many backslashes (such as file paths), use a verbatim string
@"...", where\no longer needs escaping.
Comments
Comments are notes for developers and are ignored by the compiler. C# supports three types of comments:
//: Single-line comment; everything after//to the end of the line is ignored/* */: Multi-line comment; can span multiple lines///: XML documentation comment; can generate API documentation
using System;
namespace CommentDemo
{
class Program
{
static void Main()
{
Console.WriteLine("hello");
Console.WriteLine("world");
/*
Console.WriteLine("skipped");
Console.WriteLine("also skipped");
*/
Console.WriteLine("end");
}
/// <summary>
/// Calculate the sum of two numbers
/// </summary>
static int Add(int a, int b)
{
return a + b;
}
}
}
hello
world
end
Tip: XML documentation comments (
///) are commonly used for public APIs. Visual Studio displays these descriptions when the method is called.
Top-Level Statements
Starting with C# 9, top-level statements let you skip the boilerplate namespace, class, and Main code, writing logic directly at the file's top level. The compiler automatically generates the entry method.
Example
using System;
Console.WriteLine("Top-level statement!");
int result = 40 + 2;
Console.WriteLine($"The answer is {result}");
Top-level statement!
The answer is 42
Tip: Top-level statements can only be used in one file per project. Using them in multiple files causes a compilation error.
File-Scoped Namespaces
Starting with C# 10, file-scoped namespaces let you use namespace MyApp; instead of the traditional block-style namespace, reducing one level of nesting:
namespace MyApp;
class Program
{
static void Main()
{
Console.WriteLine("File-scoped namespace");
}
}
This is equivalent to:
namespace MyApp
{
class Program
{
static void Main()
{
Console.WriteLine("File-scoped namespace");
}
}
}
Tip: File-scoped namespaces make code more concise and are recommended for new projects.
❓ FAQ
📖 Summary
- A C# program consists of using directives, namespace, class, and Main method
- Every statement must end with a semicolon
- Code blocks are wrapped in braces and can be nested
- Console.Write does not add a newline; Console.WriteLine does
- Escape characters use a backslash prefix; verbatim strings
@"..."avoid escaping - Three comment types: single-line
//, multi-line/* */, XML doc/// - Top-level statements (C# 9+) simplify entry-point code
- File-scoped namespaces (C# 10+) reduce nesting levels
📝 Exercises
- Write a console program that uses Console.Write and Console.WriteLine to output your name and age
- Use escape characters to output three lines of text in a single Console.WriteLine statement
- Rewrite a classic program structure using top-level statements
- Add XML documentation comments to a method, including
<summary>and<param>tags



