Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Explicit and implicit interface implementation in C#

Explicit interface implantation

With explicit interface implementations you can only access them when treating it as that interface.

i.e  ISedanCar.GetCarDetails(){};

Why explicit interface implantation?

If a class implements two interfaces (Multiple inheritance) and both interfaces with the same method signatures and different implementations, then you must implement explicitly.

Implicit interface implantation

Implicit is when you define your interface via a member on your class or normal class member implementation without interface name prefix.

Note:

  • There is no need to specify public/private/protected with explicit implementations since it will always be public as interfaces cannot have non-public members.
  • Interface's methods are not overridden, they are implemented, in case if you are trying child class as abstract class remember it. 

Explicit and implicit interface implementation examples

Interfaces

  interface ICar
    {
        void GetCarDetails();
    }
    interface ISedanCar
    {
        void GetCarDetails();
    }
    interface ISuvCar
    {
        void GetCarDetails();
    }

 

Implicit interface implantation example

//implicit implementation  
    public class BasicCar : ICar
    {
        //implicit         public void GetCarDetails()
        {
            Console.WriteLine("Details: Basic car features");
        }
    }  


//Hot it works?
    class Program
    {
        static void Main(string[] args)
        {
            var mycar = new BasicCar();
            mycar.GetCarDetails();
            ICar icar = new BasicCar();
            icar.GetCarDetails();
        }
    }


Output:

Details: Basic car features
Details: Basic car features

Implicit and Explicit implantation with single interface example

 //implicit and explicit implementation 
    public class Car : ISedanCar
    {
        //implicit implementation
        public void GetCarDetails()
        {
            Console.WriteLine("Details: common car features");
        }
 
        //explicit interface
        void ISedanCar.GetCarDetails()
        {
            Console.WriteLine("Details: sedan car features");
        }
    }

 

//How it works? 
    class Program
    {
        static void Main(string[] args)
        {
            Car mycar = new Car();
            mycar.GetCarDetails();
            ISedanCar mySedanCar=new Car();
            mySedanCar.GetCarDetails();
        }
    }

Output:

Details: common car features
Details: sedan car features

Explicit implantation with two interfaces (multiple inheritance) example

  //explicit implementation with multiple interface 
    public class LatestCar : ISedanCar, ISuvCar
    {
        //explicit interface implementation
        void ISedanCar.GetCarDetails()
        {
            Console.WriteLine("Details: sedan car features");
        }
 
        //explicit interface implementation
        void ISuvCar.GetCarDetails()
        {
            Console.WriteLine("Details: suv car features");
        }
    }


   //Hot it works with class instance?
    class Program
    {
        static void Main(string[] args)
        {
            var myCar = new LatestCar();
           //Showing compile time error with following
            myCar.GetCarDetails();
        }
    }


  //Hot it works with interface references?
    class Program
    {
        static void Main(string[] args)
        {
            ISedanCar mySedanCar = new LatestCar();
            mySedanCar.GetCarDetails();
            ISuvCar mySuvCar = new LatestCar();
            mySuvCar.GetCarDetails();
        }
    }


Output:

Details: sedan car features
Details: suv car features

Understanding event handling in c#

Understanding event handling in c#

What is event?

In simple English language event, some action or something to happen. 

In programming terms, it means that an object raises an event and another object or other objects handle the event.

Two types of objects communicate in event handling

1. object raises an event (Publisher)
2. objects handle the event (Subscribers)

How it works?

The object (publisher) that raises the event notifies the other objects (subscribers) that something has occurred so that the objects (subscribers) can do (handle) something on and with that event.

Delegate

Delegate is type that represents safe reference to methods with parameters list and return type.

Types (All three you can try with event handling and explained in detail with following codechef4u posts)


Events and delegates

Events and delegates are inextricably linked together, a delegate object can only refer to methods whose signature match the delegate’s.

EventHandler Delegate

Represents the method that will handle an event that has no event data.

public delegate void EventHandler(object? sender, EventArgs e); 
Object sender: The source of the event.
EventArgs e : An object that contains no event data.

In detail

.NET provides the EventHandler and EventHandler<TEventArgs> delegates to support most event scenarios. Use the EventHandler delegate for all events that do not include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event.

These delegates have no return type value and take two parameters (an object for the source of the event, and an object for event data).

C# Event Handler Example

Publisher class with eventhandler

//publisher class
   public class Logs
    {
        //Delegate use
        //public delegate void LogEventHandler(object source, LogEventArgs e);
        //event using delegate
        // public event LogEventHandler GenarateLog;
 
        public event EventHandler<LogEventArgs> GenarateLog;
        public void GenarateLogs(LogsData LogData)
        {
            Console.WriteLine("Generate logs");
            Thread.Sleep(3000);
 
            OnGenarateLog(LogData);
        }
        public virtual void OnGenarateLog(LogsData LogData)
        {
            //Legacy old invoke
            //if (GenarateLog != null)
            //{
            //    GenarateLog(this, new LogEventArgs() { LogsData = LogData });
            //}
 
            GenarateLog?.Invoke(this, new LogEventArgs() { LogsData = LogData });
        }
    }

 

Custom EventArgs implementation

//Custom event args
    public class LogEventArgs : EventArgs
    {
        public LogsData LogsData { get; set; }
    }

 

Subscriber classes

   //subscriber class 1 
    public class TextLogs
    {
        public void OnGenarateLog(object sender, LogEventArgs e)
        {
            Console.WriteLine("Generate text logs :"+e.LogsData.Title);
        }      }
 
    //subscriber class 2
    public class XmlLogs
    {
       public void OnGenarateLog(object sender, LogEventArgs e)
        {
            Console.WriteLine("Generate xml logs : "+e.LogsData.Title);
        }
    }

 

How to use it ?

class Program
    {
        static void Main(string[] args)
        {   
           //custom data instance
           LogsData logsData = new LogsData() { Title = "Event method" };
            //publisher instance
            var Logs = new Logs();
            //Text logs subscriber instance
            var textLogs = new TextLogs();
            //xml logs subscriber instance
            var xmlLogs = new XmlLogs();//xml logs subscriber
 
          //Events 
            Logs.GenarateLog += textLogs.OnGenarateLog;
            Logs.GenarateLog += xmlLogs.OnGenarateLog;
 
            Logs.GenarateLogs(logsData);
        }
    } 

Covariance and Contravariance in c#

Covariance and contravariance in generics and delegates

When you assign a method to a delegate, the method signature does not have to match the delegate exactly. This is called covariance and contravariance.

Covariance: It is related to method return types.

Enables you to use a more derived type than originally specified.

I.e You can assign an instance of IEnumerable<Derived> to a variable of type IEnumerable<Base>.

Contravariance: It is related to method parameter types.

Enables you to use a more generic (less derived) type than originally specified.

You can assign an instance of Action<Base> to a variable of type Action<Derived>.

Covariance and Contravariance c# simple example

 

  public class Employee
    {
        public string Name { get; set; }
    }
 
    public class ComtractEmployee : Employee { }
 
    public class MailingList
    {
        public void Add(IEnumerable<Employee> employee) {
 
            //some code
        }
    }
 
    public class Company
    {
        public IEnumerable<ComtractEmployee> GetContractEmployees() {
 
            var  empList = new List<ComtractEmployee>();
            //add some employee here n then return
            return empList;
        }
    }
 
    public class EmpNameComparer : IComparer<Employee>
    {
        public int Compare(Employee a, Employee b)
        {
            if (a == null) return b == null ? 0 : -1;
            return b == null ? 1 : Compare(a, b);
        }
 
        private int Compare(string a, string b)
        {
            if (a == null) return b == null ? 0 : -1;
            return b == null ? 1 : a.CompareTo(b);
        }
    }

 

class Program
    {
        static void Main(string[] args)
        {        
            //covariant use
            var Company = new Company();
            var Employees = Company.GetEmployees();
            var mailingList = new MailingList();
 
            // Add() is covariant here, we can use a more derived type
            mailingList.Add(Employees);
 
// the Set<T> constructor uses a contravariant interface, IComparer<in T>,
// we can use a more generic type than required.
var ContEmpSetSet = new SortedSet<ComtractEmployee>(Employees, new EmpNameComparer());
  }
}

Invariance

Means that you can use only the type originally specified. An invariant generic type parameter is neither covariant nor contravariant.

You cannot assign an instance of List<Base> to a variable of type List<Derived> or vice versa.