Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

OOPS in C#



Object-Oriented Programming (OOP) in C# is a paradigm that uses objects and classes to structure software programs. It is based on four key principles: Encapsulation, Inheritance, Polymorphism, and Abstraction. Here's a detailed explanation of each concept with examples:

1. Encapsulation

Encapsulation is the mechanism of hiding the internal details of an object and exposing only the necessary components. It helps in protecting the data and methods from outside interference and misuse.

Example:

csharp
public class BankAccount
{
    private decimal balance;  // Private field

    public void Deposit(decimal amount)  // Public method
    {
        if (amount > 0)
        {
            balance += amount;
        }
    }

    public decimal GetBalance()  // Public method
    {
        return balance;
    }
}

public class Program
{
    public static void Main()
    {
        BankAccount account = new BankAccount();
        account.Deposit(100);
        Console.WriteLine("Balance: " + account.GetBalance());  // Output: Balance: 100
    }
}

2. Inheritance

Inheritance allows a class to inherit properties and methods from another class. The class that inherits is called the derived class (or child class), and the class being inherited from is the base class (or parent class).

Example:

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

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

public class Program
{
    public static void Main()
    {
        Dog dog = new Dog();
        dog.Eat();  // Output: Eating...
        dog.Bark(); // Output: Barking...
    }
}

3. Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon. It can be achieved through method overriding and method overloading.

Example: Method Overriding:

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

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

public class Program
{
    public static void Main()
    {
        Animal myDog = new Dog();
        myDog.MakeSound();  // Output: Woof
    }
}

Example: Method Overloading:

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

    public double Add(double a, double b)
    {
        return a + b;
    }
}

public class Program
{
    public static void Main()
    {
        MathOperations math = new MathOperations();
        Console.WriteLine(math.Add(5, 3));      // Output: 8
        Console.WriteLine(math.Add(5.5, 3.3));  // Output: 8.8
    }
}

4. Abstraction

Abstraction involves hiding the complex implementation details and showing only the essential features of the object. It is achieved using abstract classes and interfaces.

Example: Using Abstract Class:

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

public class Circle : Shape
{
    public double Radius { get; set; }

    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }
}

public class Program
{
    public static void Main()
    {
        Circle circle = new Circle { Radius = 5 };
        Console.WriteLine("Area of Circle: " + circle.CalculateArea());  // Output: Area of Circle: 78.53981633974483
    }
}

Example: Using Interface:

csharp
public interface IShape
{
    double CalculateArea();
}

public class Rectangle : IShape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public double CalculateArea()
    {
        return Width * Height;
    }
}

public class Program
{
    public static void Main()
    {
        Rectangle rectangle = new Rectangle { Width = 5, Height = 4 };
        Console.WriteLine("Area of Rectangle: " + rectangle.CalculateArea());  // Output: Area of Rectangle: 20
    }
}

These concepts form the foundation of object-oriented programming in C#. They help in creating robust, reusable, and maintainable code. If you have any more questions or need further examples, feel free to ask! ??




All rights reserved | Privacy Policy | Sitemap