Module 3 - Chapter 1: Fundamentals of Object-Oriented Programming (OOP) in C#

Object-Oriented Programming (OOP) is a paradigm that uses "objects" to design applications and computer programs. It utilizes several principles such as encapsulation, inheritance, and polymorphism to create more modular, reusable, and flexible code. C#, being a modern object-oriented language, fully supports these concepts, making it a powerful tool for developing complex software systems. This chapter introduces the fundamentals of OOP in C#, covering classes and objects, properties and methods, constructors and destructors, and the concept of static members.

Mar 28, 2024

Fundamentals of Object-Oriented Programming (OOP) in C#

Object-Oriented Programming (OOP) is a paradigm that uses "objects" to design applications and computer programs. It utilizes several principles such as encapsulation, inheritance, and polymorphism to create more modular, reusable, and flexible code. C#, being a modern object-oriented language, fully supports these concepts, making it a powerful tool for developing complex software systems. This chapter introduces the fundamentals of OOP in C#, covering classes and objects, properties and methods, constructors and destructors, and the concept of static members.

Classes and Objects

At the heart of OOP are classes and objects. A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). An object is an instance of a class.

Defining a Class

public class Dog { // Fields, properties, methods, and events go here... public string Name; public void Bark() { Console.WriteLine("Woof!"); } }

Creating an Object

Dog myDog = new Dog(); myDog.Name = "Buddy"; myDog.Bark(); // Outputs: Woof!

Properties and Methods

Properties are special methods called accessors. This allows you to access class data safely. Methods define the behaviors or functionalities of your object.

Property Example

public class Person { private string name; // field // Property public string Name { get { return name; } set { name = value; } } }

Method Example

public class Calculator { public int Add(int a, int b) { return a + b; } }

Constructors and Destructors

A constructor is a special method of the class that is automatically called when an instance of a class is created. A destructor is used to destroy instances of a class.

Constructor Example

public class Book { public Book() // Constructor { Console.WriteLine("A new book object has been created!"); } }

Destructor Example

public class Demo { ~Demo() // Destructor { // Cleanup statements... Console.WriteLine("Destructor is called when object is destroyed."); } }

Static Members

Static members belong to the class itself instead of an instance of the class. This means that they can be called without an object.

Static Method Example

public class Utility { public static void SayHello() { Console.WriteLine("Hello!"); } }
Call a static method:
Utility.SayHello();

Summary

Understanding the fundamentals of OOP in C# is essential for building robust and scalable software. By leveraging classes and objects, encapsulating data with properties, defining behavior with methods, initializing objects with constructors, and utilizing static members, you can develop well-structured and efficient applications. This foundation will also prepare you for more advanced OOP concepts such as inheritance, polymorphism, and interfaces.