Skip to content

C# Basics

C# is a modern, object-oriented programming language that enables developers to build a variety of secure and robust applications. Understanding these fundamental concepts is essential for C# development.

Program Structure

Every C# program starts with a basic structure that includes using directives, a namespace, and a class containing the program's entry point (Main method).

csharp
using System;

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

Comments

Comments help document your code and explain complex logic. C# supports three types of comments for different documentation needs.

csharp
// Single-line comment

/* Multi-line
   comment */

/// XML documentation comment
/// Used for generating documentation

Basic Syntax

C# syntax follows consistent rules that make the code readable and maintainable. Understanding these fundamentals is crucial for writing correct C# code.

csharp
// Statements end with semicolons
int x = 5;
Console.WriteLine(x);

// Code blocks use curly braces
if (x > 0) {
    Console.WriteLine("Positive");
}

// C# is case-sensitive
string name = "John";
string Name = "Jane"; // Different variable

// Whitespace is ignored
int y=5; // Same as
int y = 5;

Each element of C# syntax serves a specific purpose:

  • Semicolons mark the end of statements
  • Curly braces define code blocks and scope
  • Case sensitivity helps distinguish between different identifiers
  • Whitespace improves readability without affecting functionality