Lesson 1
Classes and Objects in C#
Classes and Objects in C#

Let's dive into a foundational concept in Object-Oriented Programming (OOP): Classes and Objects. Whether you have explored OOP concepts in other programming languages or are encountering them for the first time, this overview will serve you well.

Object-Oriented Programming (OOP) is a programming paradigm centered around objects rather than actions and data rather than logic. It allows for organizing software design around data, or objects, rather than functions and logic.

Classes and objects are the building blocks of OOP:

  • A class acts as a blueprint for creating objects.
  • Objects are instances of a class.

Understanding these basics is essential before moving on to more advanced OOP topics like inheritance, polymorphism, and encapsulation.

Declaring and Defining Classes

In C#, a class is defined using the class keyword. Here's a simple example:

C#
1public class Person 2{ 3 // Fields of the class (name and age) 4 public string name; 5 public int age; 6 7 // Constructor that initializes the object with a name and age 8 public Person(string name, int age) 9 { 10 this.name = name; 11 this.age = age; 12 } 13 14 // Copy constructor to create a copy of the object of the same type 15 public Person(Person other) 16 { 17 name = other.name; 18 age = other.age; 19 } 20 21 // Member function to display the object's data 22 public void Display() 23 { 24 Console.WriteLine($"Name: {name}, Age: {age}"); 25 } 26}

In this snippet, we define a Person class with fields name and age, and member functions, including a constructor and a copy constructor. We also have a Display function to print the object's data.

Creating Objects from Classes

Once you have defined a class, you can create objects (instances of the class). Here’s how we can create and use objects of the Person class:

C#
1class Program 2{ 3 static void Main() 4 { 5 Person person = new Person("Alice", 30); // Creating an object 6 person.Display(); // Displaying the object's data 7 // Output: "Name: Alice, Age: 30" 8 9 Person personCopy = new Person(person); // Using the copy constructor 10 personCopy.Display(); // Displaying the copied object's data 11 // Output: "Name: Alice, Age: 30" 12 } 13}

Here, we create an object, person, with the name "Alice" and age 30, and another object, personCopy, which is a copy of the first object using the copy constructor. Both objects display their data using the Display method.

Conclusion

Understanding classes and objects is critical because they enable you to model real-world entities in your programs. For instance, a Person class helps you create multiple person objects with different names and ages, enabling you to manage and manipulate data efficiently.

This principle is the backbone of complex software systems. With this knowledge, you will be better prepared to approach more advanced OOP techniques and design patterns, allowing you to write clean, modular, and scalable code.

Let's get started with the practice section to gain more hands-on experience!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.