Generics and Generic Inheritence

Key points

				
					using ConsoleApp18;
using Employeenamespace;
using System;
using Managernamespace;

namespace Covariance
{
    class Program
    {
        static void Main(string[] args)
        {
            var employeeRepository = new GenericRepository<Employee>();
            employeeRepository.Add(new Employee { Id = 1, Name = "Adam" });
            employeeRepository.Add(new Employee { Id = 2, Name = "Abhishek" });
            employeeRepository.Add(new Employee { Id = 3, Name = "Aman" });
            employeeRepository.Add(new Employee { Id = 4, Name = "Naveen" });
            employeeRepository.Save();

            Console.WriteLine("-------------------------------");
            var managerRepository = new GenericRepository<Manager>();
            managerRepository.Add(new Manager { Id = 1, Name = "Adam1" });
            managerRepository.Add(new Manager { Id = 2, Name = "Abhishek1" });
            managerRepository.Add(new Manager { Id = 3, Name = "Aman1" });
            managerRepository.Add(new Manager { Id = 4, Name = "Naveen1" });
            managerRepository.Save();

            /*Inheritence using Generics*/
            var managerRepositoryWithRemove = new EmployeeRepositoryWithRemove<Manager>();
            //managerRepositoryWithRemove.Remove(new Manager());

            Console.WriteLine("-------------------------------");

            var managerRepositoryWithRemove1 = new EmployeeRepositoryWithRemoveMultipleKey<Manager,int>();


            Console.ReadLine();
        }
    }
}

				
			
				
					

namespace Managernamespace
{
    class Manager
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString() => $"My Name is {Name} with Id :: {Id}";
    }
}


namespace Employeenamespace
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString() => $"My Name is {Name} with Id :: {Id}";
    }
}

				
			
				
					using System;
using System.Collections.Generic;

namespace ConsoleApp18
{
    public class GenericRepository<T>
    {
        protected readonly List<T> _items = new List<T>();

        public void Add(T item)
        {
            _items.Add(item);
        }

        public void Save()
        {
            foreach (var item in _items)
            {
                Console.WriteLine(item);
            }
        }
    }

    /*Inheritence using Generics..*/
    public class EmployeeRepositoryWithRemove<T> : GenericRepository<T>
    {
        public void Remove(T item)
        {
            _items.Remove(item);
        }
    }

    /*-----------------------------------------------------------------------*/

    public class GenericRepositoryMulltipleParameter<T,TKey>
    {
        public TKey Key { get; set; } 
        protected readonly List<T> _items = new List<T>();

        public void Add(T item)
        {
            _items.Add(item);
        }

        public void Save()
        {
            foreach (var item in _items)
            {
                Console.WriteLine(item);
            }
        }
    }

    /*Inheritence using Generics..*/
    //public class EmployeeRepositoryWithRemoveMultipleKey<T,Tkey> : GenericRepositoryMulltipleParameter<T,string>
    //public class EmployeeRepositoryWithRemoveMultipleKey<T,Tkey> : GenericRepositoryMulltipleParameter<T,string>
    public class EmployeeRepositoryWithRemoveMultipleKey<T,Tkey> : GenericRepositoryMulltipleParameter<T,Tkey>
    {
        public void Remove(T item)
        {
            _items.Remove(item);
        }
    }

}

				
			

Leave a Comment