Introduction
C# is the heart of the .NET ecosystem. Whether you are building web APIs, desktop apps, or cloud services, everything starts with strong C# fundamentals.
In this article, you’ll learn:
Core building blocks of C#
How object-oriented programming works in C#
Why interfaces are important
A beginner-friendly introduction to Dependency Injection
This is Week 3 of our .NET weekly blog series.
1️⃣ Variables and Data Types
C# is a strongly typed language, meaning every variable has a specific type.
Common Data Types
int age = 25;
double price = 99.99;
bool isActive = true;
string name = "John";
var Keyword
C# can infer the type automatically:
var count = 10; // int
var title = "DotNet"; // string
✔ Use var when the type is obvious
❌ Avoid it when readability suffers
2️⃣ Control Flow (Conditions & Loops)
If–Else Statement
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
Loops
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
foreach (var item in items)
{
Console.WriteLine(item);
}
3️⃣ Methods (Functions)
Methods help you reuse code and keep it clean.
static int Add(int a, int b)
{
return a + b;
}
Calling the method:
int result = Add(5, 3);
4️⃣ Classes and Objects (OOP Basics)
C# follows Object-Oriented Programming (OOP).
Creating a Class
class User
{
public string Name { get; set; }
public int Age { get; set; }
public void Greet()
{
Console.WriteLine($"Hello, {Name}");
}
}
Creating an Object
var user = new User
{
Name = "Alice",
Age = 30
};
user.Greet();
5️⃣ Interfaces (Why They Matter)
Interfaces define what a class should do, not how it does it.
