C #
1 Introduction to C#
1.1 Overview of C#
1.2 History and Evolution of C#
1.3 NET Framework and C#
1.4 Setting Up the Development Environment
1.5 Basic Structure of a C# Program
2 C# Basics
2.1 Variables and Data Types
2.2 Operators and Expressions
2.3 Control Structures (if, else, switch)
2.4 Loops (for, while, do-while)
2.5 Arrays and Collections
3 Object-Oriented Programming in C#
3.1 Classes and Objects
3.2 Constructors and Destructors
3.3 Inheritance and Polymorphism
3.4 Encapsulation and Access Modifiers
3.5 Interfaces and Abstract Classes
3.6 Exception Handling
4 Advanced C# Concepts
4.1 Delegates and Events
4.2 Lambda Expressions
4.3 LINQ (Language Integrated Query)
4.4 Generics
4.5 Collections and Indexers
4.6 Multithreading and Concurrency
5 File Handling and Serialization
5.1 File IO Operations
5.2 Streams and ReadersWriters
5.3 Serialization and Deserialization
5.4 Working with XML and JSON
6 Windows Forms and WPF
6.1 Introduction to Windows Forms
6.2 Creating a Windows Forms Application
6.3 Controls and Event Handling
6.4 Introduction to WPF (Windows Presentation Foundation)
6.5 XAML and Data Binding
6.6 WPF Controls and Layouts
7 Database Connectivity
7.1 Introduction to ADO NET
7.2 Connecting to Databases
7.3 Executing SQL Queries
7.4 Data Adapters and DataSets
7.5 Entity Framework
8 Web Development with ASP NET
8.1 Introduction to ASP NET
8.2 Creating a Web Application
8.3 Web Forms and MVC
8.4 Handling Requests and Responses
8.5 State Management
8.6 Security in ASP NET
9 Testing and Debugging
9.1 Introduction to Unit Testing
9.2 Writing Test Cases
9.3 Debugging Techniques
9.4 Using Visual Studio Debugger
10 Deployment and Maintenance
10.1 Building and Compiling Applications
10.2 Deployment Options
10.3 Version Control Systems
10.4 Continuous Integration and Deployment
11 Exam Preparation
11.1 Overview of the Exam Structure
11.2 Sample Questions and Practice Tests
11.3 Tips for Exam Success
11.4 Review of Key Concepts
12 Additional Resources
12.1 Recommended Books and Articles
12.2 Online Tutorials and Courses
12.3 Community Forums and Support
12.4 Certification Pathways
Inheritance and Polymorphism in C#

Inheritance and Polymorphism in C#

Inheritance and Polymorphism are fundamental concepts in object-oriented programming (OOP) that allow for code reuse and flexibility. Understanding these concepts is crucial for building scalable and maintainable applications in C#.

1. Inheritance

Inheritance is a mechanism that allows a class to inherit properties and methods from another class. The class that inherits is called the derived class or subclass, and the class being inherited from is called the base class or superclass. Inheritance promotes code reuse and the creation of a hierarchical relationship between classes.

Example

class Animal
{
    public string Name { get; set; }

    public void Eat()
    {
        Console.WriteLine(Name + " is eating.");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine(Name + " is barking.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog myDog = new Dog();
        myDog.Name = "Buddy";
        myDog.Eat();  // Inherited from Animal
        myDog.Bark(); // Defined in Dog
    }
}

In this example, the Dog class inherits from the Animal class. The Dog class can use the Eat method defined in the Animal class, and it also has its own Bark method.

2. Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods to be written that can work with objects of multiple types. Polymorphism is often achieved through method overriding and interfaces.

Method Overriding

Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is done using the override keyword.

Example

class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound.");
    }
}

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

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat meows.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myAnimal.MakeSound(); // Output: The animal makes a sound.
        myDog.MakeSound();    // Output: The dog barks.
        myCat.MakeSound();    // Output: The cat meows.
    }
}

In this example, the MakeSound method is overridden in both the Dog and Cat classes. When the method is called on objects of these classes, the specific implementation for each class is executed.

Interfaces

Interfaces define a contract that classes must adhere to. A class that implements an interface must provide implementations for all its methods. This allows for polymorphism by enabling objects of different classes to be treated as objects of the interface type.

Example

interface IShape
{
    void Draw();
}

class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a circle.");
    }
}

class Square : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a square.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IShape myCircle = new Circle();
        IShape mySquare = new Square();

        myCircle.Draw(); // Output: Drawing a circle.
        mySquare.Draw(); // Output: Drawing a square.
    }
}

In this example, both the Circle and Square classes implement the IShape interface. This allows objects of these classes to be treated as IShape objects, enabling polymorphic behavior.

Conclusion

Inheritance and Polymorphism are powerful tools in C# that enhance code reuse and flexibility. By understanding and applying these concepts, you can create more modular and maintainable applications. Inheritance allows for the creation of hierarchical relationships between classes, while Polymorphism enables methods to work with objects of different types, making your code more adaptable and extensible.