This tutorial teaches C# from basics to essential concepts with examples.
C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It is widely used for:
You can write C# using:
dotnet new console -n MyApp cd MyApp dotnet run
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
Main() method.
int age = 25; double price = 99.99; char grade = 'A'; string name = "John"; bool isActive = true;
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
int number = 10;
if (number > 0)
{
Console.WriteLine("Positive");
}
else
{
Console.WriteLine("Negative");
}
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
default:
Console.WriteLine("Other day");
break;
}
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
int i = 1;
while (i <= 5)
{
Console.WriteLine(i);
i++;
}
static void Greet(string name)
{
Console.WriteLine("Hello " + name);
}
Greet("Alice");
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
Console.WriteLine(num);
}
class Person
{
public string Name;
public int Age;
public void Introduce()
{
Console.WriteLine("My name is " + Name);
}
}
Person p = new Person();
p.Name = "Sarah";
p.Age = 30;
p.Introduce();
class Car
{
public string Model;
public Car(string model)
{
Model = model;
}
}
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
try
{
int x = int.Parse("abc");
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
using System.Collections.Generic; Listnames = new List (); names.Add("Alice"); names.Add("Bob"); foreach (string n in names) { Console.WriteLine(n); }
C# is powerful, flexible, and widely used across many platforms. Practice regularly and build small projects to master it.
Next Topics to Learn: