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.
Interface Example
public interface IEmailService
{
void SendEmail(string to, string message);
}
Implementing an Interface
public class EmailService : IEmailService
{
public void SendEmail(string to, string message)
{
Console.WriteLine("Email sent");
}
}
✔ Helps with loose coupling
✔ Makes code testable and maintainable
6️⃣ Introduction to Dependency Injection (DI)
Dependency Injection is a core concept in modern .NET apps.
❌ Without DI (Bad Practice)
public class OrderService
{
private EmailService _emailService = new EmailService();
}
✅ With DI (Recommended)
public class OrderService
{
private readonly IEmailService _emailService;
public OrderService(IEmailService emailService)
{
_emailService = emailService;
}
}
Registering in .NET
builder.Services.AddScoped<IEmailService, EmailService>();
7️⃣ Value Types vs Reference Types
| Type | Example |
| -------------- | -------------------- |
| Value Type | int, bool, struct |
| Reference Type | class, array, string |
int a = 5;
int b = a;
b = 10; // a remains 5
var obj1 = new User();
var obj2 = obj1;
obj2.Name = "Bob"; // affects obj1
8️⃣ Common Beginner Mistakes
❌ Ignoring null values
❌ Not using interfaces
❌ Writing large methods
❌ Skipping DI concepts
Best Practices for C# Beginners
✅ Follow naming conventions
✅ Keep methods small
✅ Use interfaces
✅ Learn async/await early
✅ Read error messages carefully
What You’ve Learned
✔ Variables & data types
✔ Control flow
✔ Methods
✔ Classes & objects
✔ Interfaces
✔ Dependency Injection basics
These concepts are mandatory for ASP.NET Core development.