Skip to main content

C# Core Study Guide

Overview

C# (pronounced C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It is widely used for:

  • Backend development with ASP.NET Core
  • Desktop applications
  • Cloud services
  • Game development with Unity
  • Enterprise software
  • APIs and microservices

C# runs on the .NET platform and provides powerful features such as:

  • Automatic memory management
  • Strong type safety
  • Asynchronous programming
  • LINQ for data querying
  • Cross-platform support

Why Learn C#

Advantages

FeatureDescription
Object-OrientedEncourages reusable and maintainable code
Cross-PlatformWorks on Windows, Linux, and macOS
High PerformanceOptimized runtime with JIT compilation
Rich EcosystemLarge .NET ecosystem and libraries
Enterprise ReadyWidely used in enterprise systems
Cloud FriendlyExcellent integration with Azure

.NET Ecosystem

Installing the Environment

Install .NET SDK

Official website: https://dotnet.microsoft.com/download

Verify installation:

dotnet --version

Your First C# Program

using System;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
KeywordMeaning
usingImports namespaces
classDefines a class
staticBelongs to the class itself
voidNo return value
MainEntry point of application

Variables and Data Types

int age = 25;
string name = "John";
double salary = 5000.50;
bool isActive = true;
TypeDescriptionExample
intInteger10
doubleFloating point10.5
decimalFinancial calculations99.99m
boolTrue/Falsetrue
charSingle character'A'
stringText"Hello"

Type Conversion

Implicit Casting

int number = 10;
double value = number;

Explicit Casting

double price = 9.99;
int rounded = (int)price;

Parsing

string text = "123";
int number = int.Parse(text);

Operators

Arithmetic Operators

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus

Comparison Operators

int a = 10;
int b = 20;

Console.WriteLine(a < b);
Console.WriteLine(a == b);

Conditional Statements

if Statement

int age = 18;

if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}

switch Statement

int day = 2;

switch(day)
{
case 1:
Console.WriteLine("Monday");
break;

case 2:
Console.WriteLine("Tuesday");
break;

default:
Console.WriteLine("Unknown");
break;
}

Loops

for Loop

for(int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}

while Loop

int count = 0;

while(count < 5)
{
Console.WriteLine(count);
count++;
}

foreach Loop

string[] fruits = { "Apple", "Orange", "Banana" };

foreach(var fruit in fruits)
{
Console.WriteLine(fruit);
}

Methods

static int Add(int a, int b)
{
return a + b;
}

int result = Add(5, 3);
Console.WriteLine(result);

Object-Oriented Programming (OOP)

PrincipleDescription
EncapsulationHiding internal details
InheritanceReusing code from base classes
PolymorphismMultiple behaviors
AbstractionHiding complexity

Classes and Objects

class Person
{
public string Name;
public int Age;

public void Introduce()
{
Console.WriteLine($"My name is {Name}");
}
}

Person person = new Person();
person.Name = "Alice";
person.Age = 30;
person.Introduce();

Constructors

class Car
{
public string Brand;

public Car(string brand)
{
Brand = brand;
}
}

Properties

class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; }
}

Access Modifiers

ModifierAccess Level
publicAccessible everywhere
privateAccessible only inside class
protectedAccessible in derived classes
internalAccessible within assembly

Inheritance

class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}

class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}

Polymorphism

class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}

class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}

Abstraction

abstract class Shape
{
public abstract double CalculateArea();
}

Interfaces

interface ILogger
{
void Log(string message);
}

class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}

Exception Handling

try
{
int result = 10 / 0;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finished");
}

Collections

List<string> names = new List<string>();
names.Add("John");
names.Add("Alice");

Dictionary<int, string> users = new Dictionary<int, string>();
users.Add(1, "Admin");

LINQ

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

var evenNumbers = numbers.Where(n => n % 2 == 0);

foreach(var number in evenNumbers)
{
Console.WriteLine(number);
}

Async and Await

public async Task FetchDataAsync()
{
await Task.Delay(1000);

Console.WriteLine("Data fetched");
}

Benefits:

  • Non-blocking operations
  • Better scalability
  • Improved responsiveness

File Handling

File.WriteAllText("data.txt", "Hello");

string content = File.ReadAllText("data.txt");

Dependency Injection (DI)

services.AddScoped<IUserService, UserService>();

Benefits: loose coupling, easier testing, better maintainability.


Nullable Types

int? number = null;

string name = input ?? "Default";

Memory Management

C# uses automatic garbage collection to free unused memory.

Best practices:

  • Dispose of unmanaged resources with using statements
  • Avoid memory leaks by releasing subscriptions and event handlers

/src
/Controllers
/Services
/Repositories
/Models
/Interfaces
/DTOs

Real-World Use Cases

Use CaseTechnology
REST APIsASP.NET Core
Enterprise Apps.NET
Cloud ServicesAzure
GamesUnity
Desktop AppsWPF / WinForms

Interview Questions

Beginner

  1. What is C#?
  2. Difference between class and object?
  3. What is inheritance?
  4. What is polymorphism?
  5. What is an interface?

Intermediate

  1. Difference between abstract class and interface?
  2. What is dependency injection?
  3. Explain async/await.
  4. What is LINQ?
  5. What is garbage collection?

Advanced

  1. Explain CLR and JIT.
  2. What are delegates and events?
  3. What is middleware in ASP.NET Core?
  4. Explain memory management in .NET.
  5. What are expression trees?

Learning Roadmap