Object-Oriented Programming (OOP) Basics in C#

Object-Oriented Programming (OOP) Basics in C#

Object-Oriented Programming (OOP) Basics

Introduction to OOP

Object-Oriented Programming (OOP) is a programming paradigm that organizes code into reusable and interconnected units called objects. These objects represent real-world entities or concepts, making it easier to design, build, and maintain complex software systems. OOP is widely used in software development due to its ability to model real-world scenarios, promote code reuse, and improve code readability.

OOP is built on four key principles:

Encapsulation: Bundling data (fields) and behavior (methods) into a single unit (class) and restricting access to internal details.

  • Real-world analogy: A television. You use buttons to operate it, but you don’t need to understand its internal wiring.

Inheritance: Allowing a class (child) to inherit properties and behavior from another class (parent).

  • Real-world analogy: A car (child class) inherits general characteristics like wheels and an engine from a vehicle (parent class).

Polymorphism: Enabling a single function or method to behave differently based on the context.

  • Real-world analogy: A person (object) can act as a teacher in a classroom and as a parent at home.

Abstraction: Hiding complex details and showing only the essential features of an object.

  • Real-world analogy: When you drive a car, you focus on the steering wheel and pedals, not the engine mechanics.

Classes and Objects

In C#, a class is a blueprint for creating objects, while an object is an instance of a class.

Syntax to Define and Instantiate a Class

// Define a class
public class Person
{
    public string Name;
    public int Age;
}

// Create an object
Person person1 = new Person();
person1.Name = "Alice";
person1.Age = 25;
        

Example

Let’s create a class to represent a car:

public class Car
{
    public string Brand;
    public string Model;
    public int Year;
}

Car car1 = new Car();
car1.Brand = "Toyota";
car1.Model = "Corolla";
car1.Year = 2020;
        

Fields and Properties

Fields

Fields store data for an object. They are variables defined within a class.

public class Animal
{
    public string Name;
    public int Age;
}
        

Properties

Properties control access to fields, allowing validation or additional logic using get and set accessors.

public class Animal
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

Animal animal = new Animal();
animal.Name = "Elephant";
Console.WriteLine(animal.Name); // Outputs: Elephant
        

Constructors

A constructor is a special method used to initialize objects when they are created.

Default Constructor

public class Book
{
    public string Title;
    public Book()
    {
        Title = "Untitled";
    }
}

Book book1 = new Book();
Console.WriteLine(book1.Title); // Outputs: Untitled
        

Parameterized Constructor

public class Book
{
    public string Title;
    public Book(string title)
    {
        Title = title;
    }
}

Book book2 = new Book("C# for Beginners");
Console.WriteLine(book2.Title); // Outputs: C# for Beginners
        

Encapsulation and Access Modifiers

Encapsulation

Encapsulation restricts direct access to object data by using private fields and exposing them through public properties.

public class BankAccount
{
    private double balance;

    public double Balance
    {
        get { return balance; }
        set
        {
            if (value >= 0)
                balance = value;
        }
    }
}

BankAccount account = new BankAccount();
account.Balance = 100;
Console.WriteLine(account.Balance); // Outputs: 100
        

Access Modifiers

  • public: Accessible from anywhere.
  • private: Accessible only within the class.
  • protected: Accessible within the class and derived classes.
  • internal: Accessible within the same assembly.

Example:

public class Example
{
    private int privateField;
    public int PublicField;
}
        

By practicing these exercises, you will strengthen your understanding of OOP principles in C# and become more confident in applying them to real-world problems.


Citation and Reference:-

  • Microsoft Learn - Introduction to Object-Oriented Programming Learn the fundamentals of OOP in C#, including classes, objects, and principles. Visit: Microsoft OOP Guide
  • GeeksforGeeks - OOPs Concepts in C# A detailed explanation of OOP concepts with examples. Visit: GeeksforGeeks C# OOP Concepts
  • W3Schools - C# OOP Explore Object-Oriented Programming with easy-to-follow examples. Visit: W3Schools C# OOP
  • TutorialsPoint - C# Object-Oriented Programming Comprehensive tutorials on OOP concepts in C#. Visit: TutorialsPoint C# OOP
  • DotNetPerls - C# Classes and Objects Practical insights into creating and using classes and objects in C#. Visit: DotNetPerls C# Classes

Amit Kumar

Automation QA Engineer | Cypress.io | Robot Framework | Playwright | JavaScript | Cucumber BDD | Blockchain & AI Enthusiast

2 个月

Useful tips

要查看或添加评论,请登录

Shubham Raj的更多文章

社区洞察

其他会员也浏览了