Skip to main content

Posts

Showing posts from May, 2025

Generics

Reusability in C#: C# lets you  reuse  code in two major ways: Inheritance Generics Let’s look at each: 1.  Inheritance: Inheritance is an  object-oriented  concept. You write a  base class  and then extend it to create more  specific classes . Example:      class Animal       {            public void Eat() { Console.WriteLine("Eating..."); }      }      class Dog : Animal       {            public void Bark() { Console.WriteLine("Barking..."); }      }      // Reusing code via inheritance     Dog d = new Dog();     d.Eat(); // From Animal     d.Bark(); // From Dog ✅  Pros : Useful when classes share common behavior. ❌  Cons : Less flexible when working with  types  (like  int ,  string , etc.) 2.  Generics: Gen...