C# Tutorial for Beginners (2026): Complete Guide with Examples & Best Practices
Introduction
C# (pronounced C Sharp) is one of the most popular and versatile programming languages for building modern applications. Developed by Microsoft in 2000, C# is an object-oriented, type-safe, and high-performance programming language that runs on the .NET platform.
Today, C# is widely used to develop Windows desktop applications, web applications, cloud services, enterprise software, mobile apps, APIs, games with Unity, and AI-powered applications.
If you’re planning to become a .NET Developer, Game Developer, or Full-Stack Developer, learning C# is an excellent career choice. This guide covers everything from the basics to advanced concepts with practical examples and industry best practices.
What is C#?
C# is a modern, object-oriented, general-purpose programming language developed by Microsoft as part of the .NET framework.
It combines the simplicity of Java with the power of C++, offering automatic memory management, strong typing, and a rich standard library.
Key Characteristics
- Object-Oriented Programming (OOP)
- Strongly Typed Language
- Cross-Platform Development
- Automatic Garbage Collection
- Rich .NET Libraries
- High Performance
- Secure Programming
- Modern Language Features
History of C#
C# was created by Anders Hejlsberg and his team at Microsoft and officially released in 2002 with the .NET Framework.
Over the years, C# has evolved significantly with features introduced in:
- C# 2.0
- C# 3.0 (LINQ)
- C# 5.0 (Async/Await)
- C# 7.0
- C# 8.0
- C# 9.0
- C# 10
- C# 11
- C# 12
These versions introduced improved performance, simplified syntax, nullable reference types, records, pattern matching, and many other modern programming features.
Why Learn C#?
Learning C# offers many benefits:
- Easy to learn
- Excellent for beginners
- Strong Object-Oriented support
- Cross-platform development using .NET
- Large developer community
- Used in enterprise software
- Perfect for Unity game development
- High demand in the job market
- Secure programming language
- Rich ecosystem and libraries
Features of C#
Object-Oriented Programming
Supports encapsulation, inheritance, abstraction, and polymorphism.
Type Safety
Reduces runtime errors through compile-time type checking.
Automatic Memory Management
The .NET Garbage Collector automatically manages unused memory.
Cross-Platform Support
Applications can run on Windows, Linux, and macOS using .NET.
Language Integrated Query (LINQ)
Makes querying collections and databases simple and readable.
Asynchronous Programming
Supports efficient asynchronous programming using async and await.
Exception Handling
Provides structured error handling with try, catch, and finally.
Applications of C#
C# is widely used for:
- Windows Applications
- ASP.NET Web Applications
- REST APIs
- Enterprise Software
- Cloud Applications
- Mobile Apps (.NET MAUI)
- Unity Game Development
- Desktop Applications
- AI Applications
- Business Software
Installing C#
Install one of the following:
- .NET SDK
- Visual Studio
- Visual Studio Code + C# Extension
- JetBrains Rider
Verify installation:
dotnet --version
Your First C# Program
Create a file named:
Program.cs
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, GuruGyaan!");
}
}
Run:
dotnet run
Output
Hello, GuruGyaan!
Basic Structure of a C# Program
using System;
class Program
{
static void Main()
{
Console.WriteLine("Welcome to C# Programming");
}
}
Explanation
using System;imports the System namespace.class Programdefines the program class.Main()is the application’s entry point.Console.WriteLine()displays output.
Variables
using System;
class Program
{
static void Main()
{
int age = 25;
double salary = 45000.50;
char grade = 'A';
bool active = true;
string name = "GuruGyaan";
Console.WriteLine(name);
Console.WriteLine(age);
}
}
Data Types
| Data Type | Description |
|---|---|
| int | Integer |
| double | Decimal Number |
| float | Floating Point |
| decimal | High Precision Decimal |
| char | Character |
| bool | Boolean |
| string | Text |
Example
int number = 100;
double price = 99.99;
decimal amount = 5000.75m;
char grade = 'A';
bool status = true;
string website = "GuruGyaan";
Constants
const double PI = 3.14159;
Operators
Arithmetic Operators
int a = 20;
int b = 10;
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);
Console.WriteLine(a / b);
Other operators include:
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Null-Coalescing Operators
User Input
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
Console.WriteLine("Hello " + name);
Conditional Statements
if…else
if(age >= 18)
{
Console.WriteLine("Eligible");
}
else
{
Console.WriteLine("Not Eligible");
}
switch
switch(choice)
{
case 1:
Console.WriteLine("Add");
break;
case 2:
Console.WriteLine("Delete");
break;
default:
Console.WriteLine("Invalid Choice");
break;
}
Loops
For Loop
for(int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
While Loop
int i = 1;
while(i <= 5)
{
Console.WriteLine(i);
i++;
}
Do While Loop
int i = 1;
do
{
Console.WriteLine(i);
i++;
} while(i <= 5);
Methods
static int Add(int a, int b)
{
return a + b;
}
Console.WriteLine(Add(10,20));
Arrays
int[] marks = {80,85,90,88,95};
foreach(var mark in marks)
{
Console.WriteLine(mark);
}
Strings
string website = "GuruGyaan";
Console.WriteLine(website.ToUpper());
Console.WriteLine(website.Length);
Classes and Objects
class Student
{
public string Name = "";
public void Display()
{
Console.WriteLine(Name);
}
}
Student s = new Student();
s.Name = "Rahul";
s.Display();
Constructor
class Student
{
public Student()
{
Console.WriteLine("Constructor Called");
}
}
Inheritance
class Animal
{
public void Sound()
{
Console.WriteLine("Animal Sound");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Woof");
}
}
Polymorphism
class Animal
{
public virtual void Sound()
{
Console.WriteLine("Animal");
}
}
class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Dog");
}
}
Encapsulation
class Student
{
private int age;
public void SetAge(int value)
{
age = value;
}
public int GetAge()
{
return age;
}
}
Abstraction
abstract class Animal
{
public abstract void Sound();
}
class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Woof");
}
}
Interface
interface IAnimal
{
void Sound();
}
class Dog : IAnimal
{
public void Sound()
{
Console.WriteLine("Woof");
}
}
Exception Handling
try
{
int result = 10 / 0;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Completed");
}
File Handling
using System.IO;
File.WriteAllText("demo.txt","Welcome to GuruGyaan");
string text = File.ReadAllText("demo.txt");
Console.WriteLine(text);
Collections
List<string> students = new List<string>();
students.Add("Rahul");
students.Add("Amit");
foreach(var student in students)
{
Console.WriteLine(student);
}
LINQ
var evenNumbers = numbers.Where(x => x % 2 == 0);
foreach(var number in evenNumbers)
{
Console.WriteLine(number);
}
Asynchronous Programming
async Task LoadData()
{
await Task.Delay(1000);
Console.WriteLine("Data Loaded");
}
Best Practices
Follow these professional coding practices:
- Use meaningful variable and method names.
- Follow Microsoft’s C# naming conventions.
- Keep methods short and focused.
- Use
varonly when the type is obvious. - Prefer properties over public fields.
- Dispose unmanaged resources using
using. - Handle exceptions gracefully.
- Use async/await for I/O operations.
- Avoid duplicate code.
- Enable nullable reference types.
- Write unit tests.
- Keep classes focused on a single responsibility.
Common Programming Mistakes
- Ignoring null reference checks
- Catching generic exceptions unnecessarily
- Not disposing resources
- Creating large methods
- Using global state excessively
- Blocking asynchronous code
- Ignoring compiler warnings
Mini Project – Simple Calculator
using System;
class Program
{
static void Main()
{
Console.Write("Enter first number: ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter operator (+,-,*,/): ");
char op = Convert.ToChar(Console.ReadLine());
Console.Write("Enter second number: ");
int b = Convert.ToInt32(Console.ReadLine());
switch(op)
{
case '+':
Console.WriteLine(a+b);
break;
case '-':
Console.WriteLine(a-b);
break;
case '*':
Console.WriteLine(a*b);
break;
case '/':
Console.WriteLine(b != 0 ? a/b : "Division by zero");
break;
default:
Console.WriteLine("Invalid Operator");
break;
}
}
}
C# vs Java
| Feature | C# | Java |
|---|---|---|
| Platform | .NET | JVM |
| Garbage Collection | Yes | Yes |
| LINQ | Yes | No |
| Properties | Yes | Getters/Setters |
| Unity Support | Yes | No |
Real-World Applications
- ASP.NET Core Web Applications
- REST APIs
- Windows Desktop Applications
- Unity Games
- Enterprise Software
- Cloud Applications (Azure)
- Mobile Apps (.NET MAUI)
- Banking Software
- Healthcare Systems
- Business Management Software
Frequently Asked Questions (FAQ)
Is C# easy to learn?
Yes. C# has a clean syntax and excellent documentation, making it beginner-friendly.
Is C# still in demand?
Yes. C# is widely used for enterprise development, web APIs, cloud applications, and game development with Unity.
Can I build websites with C#?
Yes. ASP.NET Core is one of the most popular frameworks for building modern web applications and APIs.
Is C# better than Java?
Both are excellent languages. C# integrates closely with the .NET ecosystem, while Java is popular for enterprise applications and Android development.
Conclusion
C# is a modern, secure, and high-performance programming language that empowers developers to build everything from desktop software and web applications to cloud services and games. By mastering variables, methods, object-oriented programming, LINQ, collections, asynchronous programming, and .NET best practices, you’ll be well-equipped to develop scalable, maintainable, and professional applications.