C# Basic Syntax

Essential C# syntax and program structure

Program Structure

using System; # import namespace
namespace MyApp { # define namespace
    class Program { # define class
        static void Main(string[] args) { # entry point
            Console.WriteLine("Hello World!"); # output to console
        }
    }
}

Variables

int age = 25; # integer variable
double price = 99.99; # floating point
string name = "John"; # string variable
bool isActive = true; # boolean value
var number = 10; # implicit typing
const int MAX = 100; # constant value

Input and Output

Console.WriteLine("Text"); # print with newline
Console.Write("Text"); # print without newline
string input = Console.ReadLine(); # read user input
Console.WriteLine($"Name: {name}"); # string interpolation

Comments

// single line comment
/* multi-line
   comment */
/// XML documentation comment