C# Fundamentals and Object-Oriented Programming (OOP)
From the Asp. Net, mvc, c# basic 00 to advance curriculum
TL;DR
C# fundamentals cover basic syntax, data types, and control flow, which are the building blocks of any program. OOP introduces concepts like classes and objects to help you organize code into reusable, modular pieces. Mastering these concepts is crucial for writing efficient, maintainable, and scalable ASP.NET applications.
1. The Mental Model
Think of C# fundamentals as learning the alphabet and grammar of a language. OOP is like learning how to write essays and novels using that language, structuring your thoughts into logical chapters and characters for better readability and reusability.
2. The Core Material
C# is a versatile programming language developed by Microsoft. It's the primary language for ASP.NET web development.
2.1 C# Fundamentals: The Basics

Photo by Miguel Á. Padriñán on Pexels
- Variables and Data Types: Variables store data. C# is strongly typed, meaning you must declare the type of data a variable will hold.
csharp int age = 30; // Integer whole number string name = "Alice"; // Text bool isActive = true; // True/False value double price = 19.99; // Decimal numbers char initial = 'A'; // Single character - Operators: Symbols that perform operations on values.
- Arithmetic:
+,-,*,/,%(modulus) - Comparison:
==(equal to),!=(not equal to),<,>,<=,>= - Logical:
&&(AND),||(OR),!(NOT)
- Arithmetic:
-
Control Flow: Dictates the order in which your code executes.
-
Conditional Statements:
if,else if,else,switch
```csharp
if (age >= 18) {
Console.WriteLine("Adult");
} else {
Console.WriteLine("Minor");
}string day = "Monday";
switch (day) {
case "Monday":
Console.WriteLine("Start of week");
break;
default:
Console.WriteLine("Other day");
break;
}
* **Loops:** `for`, `while`, `do-while`, `foreach` (for collections)csharp
for (int i = 0; i < 5; i++) {
Console.WriteLine(i); // Prints 0, 1, 2, 3, 4
}string[] fruits = {"apple", "banana", "cherry"};
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
* **Methods (Functions):** Blocks of code that perform a specific task. They promote reusability.csharp
public int AddNumbers(int a, int b) {
return a + b;
}
// Calling the method
int sum = AddNumbers(5, 10); // sum will be 15
``` -
2.2 Object-Oriented Programming (OOP)

Photo by Rashed Paykary on Pexels
OOP is a programming paradigm based on the concept of "objects," which can contain data and code. It aims to make software more modular, flexible, and maintainable. The four pillars of OOP are:
- Encapsulation: Bundling data (attributes) and methods (behaviors) that operate on the data into a single unit (class). It hides the internal state of an object and only exposes necessary functionalities. You'll often use
privateandpublicaccess modifiers for this.
```csharp
public class Car {
private string _model; // Private field
public string Color { get; set; } // Public property (gets & sets _color implicitly)public Car(string model, string color) { // Constructor _model = model; Color = color; } public void Drive() { // Public method Console.WriteLine($"Driving the {_model} {Color} car!"); }}
2. **Abstraction:** Hiding complex implementation details and showing only the essential features of an object. Think of driving a car: you use the steering wheel and pedals (the abstraction), without needing to understand the engine's mechanics (the hidden complexity). Interfaces and abstract classes are key to abstraction in C#. 3. **Inheritance:** Allows a class (child/derived class) to inherit properties and methods from another class (parent/base class). This promotes code reuse and establishes an "is-a" relationship (e.g., a "SportsCar" *is a* "Car").csharp
public class SportsCar : Car { // SportsCar inherits from Car
public SportsCar(string model, string color) : base(model, color) {
// Calls the base class (Car) constructor
}public void Boost() { Console.WriteLine("Engaging turbo boost!"); }}
```
4. Polymorphism: Meaning "many forms." It allows objects of different classes to be treated as objects of a common base class. This enables you to write flexible and extensible code, often achieved through method overriding (same method name, different implementation in derived classes) or interfaces.
Here's how OOP concepts relate:
graph TD
A["Problem/Requirement"] --> B{Identify Entities/Objects};
B --> C["Define Classes (Templates)"];
C --> D["Encapsulation (Attributes + Methods)"];
D --> E["Abstraction (Show essential, hide complexity)"];
E --> F{Need specific variations?};
F -- Yes --> G["Inheritance (Share common behavior)"];
F -- No --> H["Create Objects (Instances)"];
G --> I["Polymorphism (Treat objects uniformly)"];
I --> H;
H --> J["Use Objects in Program Logic"];
3. Worked Example
Let's create a simple scenario using C# fundamentals and OOP to manage different types of employees.
using System;
// Base class for all employees (Encapsulation, Abstraction potential)
public class Employee {
public string Name { get; set; }
public decimal BaseSalary { get; set; }
public Employee(string name, decimal baseSalary) {
Name = name;
BaseSalary = baseSalary;
}
// Virtual method allows overriding in derived classes (Polymorphism)
public virtual decimal CalculatePay() {
return BaseSalary;
}
public void DisplayInfo() {
Console.WriteLine($"Name: {Name}, Base Salary: {BaseSalary:C}");
}
}
// Derived class for Manager (Inheritance)
public class Manager : Employee {
public decimal Bonus { get; set; }
public Manager(string name, decimal baseSalary, decimal bonus)
: base(name, baseSalary) { // Call base constructor
Bonus = bonus;
}
// Override CalculatePay for Managers (Polymorphism)
public override decimal CalculatePay() {
return BaseSalary + Bonus;
}
}
// Derived class for Developer (Inheritance)
public class Developer : Employee {
public int HoursWorked { get; set; }
public decimal HourlyRate { get; set; }
public Developer(string name, decimal baseSalary, int hoursWorked, decimal hourlyRate)
: base(name, baseSalary) {
HoursWorked = hoursWorked;
HourlyRate = hourlyRate;
}
// Override CalculatePay for Developers (Polymorphism)
public override decimal CalculatePay() {
// Assume BaseSalary here is a base monthly rate, and we add hourly pay
return BaseSalary + (HoursWorked * HourlyRate);
}
}
public class Program {
public static void Main(string[] args) {
// Creating objects (instantiation)
Employee emp1 = new Employee("John Doe", 3000m);
Manager mgr1 = new Manager("Jane Smith", 5000m, 1000m);
Developer dev1 = new Developer("Peter Jones", 2000m, 160, 25m);
Console.WriteLine("--- Employee Information ---");
// Using Polymorphism: treating derived classes as the base Employee type
Employee[] employees = new Employee[] { emp1, mgr1, dev1 };
foreach (var emp in employees) {
emp.DisplayInfo();
Console.WriteLine($"Calculated Pay: {emp.CalculatePay():C}\n");
}
// Example of direct usage
mgr1.DisplayInfo();
Console.WriteLine($"Manager Jane's Total Pay: {mgr1.CalculatePay():C}");
mgr1.Bonus = 1200m; // Modifying property
Console.WriteLine($"Manager Jane's New Total Pay: {mgr1.CalculatePay():C}\n");
dev1.DisplayInfo();
Console.WriteLine($"Developer Peter's Total Pay: {dev1.CalculatePay():C}");
}
}
Output of the code above:
```
--- Employee Information ---
Name: John Doe, Base Salary: $3,000.00
Calculated Pay: $3,000.00
Name: Jane Smith, Base Salary: $5,000.00
Calculated Pay: $6,000.00
Name: Peter Jones, Base Salary: $2,000.00
Calculated Pay: $6,000.00
Name: Jane Smith, Base Salary: $5,000.00
Manager Jane's Total Pay: $6,000.00
Manager Jane's New Total Pay: $6,200.00
Name: Peter Jones, Base Salary: $2,000.00
Developer Peter's Total Pay:
Frequently asked about C# Fundamentals and Object-Oriented Programming (OOP)
Study this next
Get the full Asp. Net, mvc, c# basic 00 to advance curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free