Why use interfaces

This a general overview about what are the advantages for using interfaces in C#. Here are some the many reason fro which we should consider using an interface while development.

  • To make use of multiple inheritance
  • C# does not allow multiple class inheritance. We can derive from only one class at a time, but not more. We can indeed inherit our class, interface from multiple interfaces.

    public class ClassB : ClassA, InterFaceA, InterFaceB
    {
     //
    }
    
  • If we expect similar behaviour from many classes, it is better to inherit them from an Interface. This makes it easy for plug-in style development and future extension of components.
  • Interfaces provide flexibility and support for Unit Testing. Most of the Testing Frameworks rely on Interfaces for Mock objects , which are created and provided with the dependencies inside the Test code Block. Instead of creation from classes, these Mocking frameworks need an Interface.
  • Interface allow us maximum flexibility for the adoption of change, without the much headache and breaking builds.
  • Interfaces are more flexible than base classes. We can define a single implementation that can implement multiple interfaces.
  • Inversion of control and dependency Injection totally rely over interfaces.
  • A very important building unit, Struct, cannot inherrit from classes but can inherit from an Interface.
  • Consider this example:
    You have a class that is capable of playing media files(mp3). You give that class to you friend who tries to play MPEG type of files. It would not be possible for him to do so without making significant changes to you class.

    public class MusicPlayer 
    {
       void Play(Mp3 _mp3File){}
    }
    

    Now consider this
    Instead of passing type of mp3 file to Play Method what if you pass this Method, a derived from an interface of Type MediaType.

    public interface MediaType
    { }
    
    public class Mp3 : MediaType
    { }
    
    public class MPEG : MediaType
    { }
    
    public class MusicPlayer 
    {
       void Play(MediaType _mediaFile){}
    }
    

    In this scenario, you can derive another MediaFile type and from MediaType like MPEG and pass that to the Play Method and it will happily accept it and play it for you (provided logic).

    public class TestPlayers
        {
            public void PlayMedia()
            {
                MusicPlayer musicPlayer = new MusicPlayer();
                musicPlayer.Play(new Mp3());
                musicPlayer.Play(new MPEG());
            }       
        }
    

    for reference and more information:

  • Significance of Interfaces C#
  • Advertisements

    Check if class(T) inherits an interface(T)

    The very first thing which comes to our mind is is operator like

    bool IsDerived = a class is Interface

    Well, that is not correct. why ? because

    is operator is used to check whether the run-time type of an object
    is compatible with a given type.

    An expression where the use of is conforms to the syntax, evaluates to true, if both of the following conditions are met:

  • expression is not null.
  • expression can be cast to type. That is, a cast expression of the form
    (type)(expression) will complete without throwing an exception. For
    more information, see 7.6.6 Cast
    expressions.
  • Solution is to use:

    bool IsDerived = typeof(ISomeInterface).IsAssignableFrom(typeof(T));

    References

  • Does a Type Implement an Interface?
  • is operator
  • A look at IEnumerator and IEnumerable interfaces in C#

    There are two interfaces which are at the base of many classes and the logic we use daily, to implement .Net based applications. These two interfaces are closely related to each other and provide a fuctionality that makes working with a Collection, fun. These are interfaces are IEnumerator and IEnumerable

    Both of these interfaces work together. We need a class to implement IEnumerator interface, which provides implementations for Current property , MoveNext() and Reset() methods like below.

    First, consider a class Job, around which all the logic will surround

        public class Job
        {
            public Job(string _jobName, int _duration)
            {
                this.jobName = _jobName;
                this.duration = _duration;
            }
    
            public string jobName;
            public int duration;
        }
    

    Now, we have class that implements IEnumerator with an underlying array of Job objects:

        class JobsList : IEnumerator
        {
            Job[] jobs;
            int position = -1;
    
            public JobsList(Job[] _jobs)
            {
                jobs = _jobs;
            }
    
            #region IEnumerator Members
    
            public object Current
            {
                get
                {
                    try
                    {
                        return jobs[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
    
            public bool MoveNext()
            {
                position++;
                return (position < jobs.Length);
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            #endregion
        }
    

    Now consider another class Jobs. This class implements IEnumerable interface method GetEnumerator() returns IEnummerator object which holds indexed access to underlying array of Job objects as follows:

        class Jobs : IEnumerable
        {
            #region IEnumerable Members
    
            Job[] jobs;
    
            public Jobs(Job[] _jobsArray)
            {
                jobs = new Job[_jobsArray.Length];
    
                for (int i = 0; i < _jobsArray.Length; i++)
                {
                    jobs[i] = _jobsArray[i];
                }
            }
    
            public IEnumerator GetEnumerator()
            {
                return new JobsList(jobs);
            }
    
            #endregion
        }
    

    This is how now can use and manipulate our newly created IEnumerator object

            static void Main(string[] args)
            {
                // create new Job Array
                Job[] jobsArray = 
                     new Job[] { new Job("JobA", 1), new Job("JobB", 2), new Job("JobC", 3) };
    
                // // create new IEnumerable 
                Jobs jobs = new Jobs(jobsArray);
    
                // // create new IEnumerator
                IEnumerator jobsEnummerator = jobs.GetEnumerator();
    
                // following are some you can work over 
                // ways we can works over IEnumerator object
                while (jobsEnummerator.MoveNext())
                { }
    
                //  or using foreach
                foreach (Job job in jobs)
                { }
            }
    

    Implicit versus Explicit implementation of Interfaces in C#

    What is the differences in implementing an interfaces implicitly or explicitly in C#?

    Suppose we have an interface IWriter as follows

        public interface IWriter
        {
            public void WrtiteTo()
            { }
        }
    

    Implicit: When we implement an interface implicitly, the implementing class exposes new behaviours. It is simply adding the methods, properties or other items required by the interface directly to the class and as public methods. So winding up, A member does this work for you in class which is implementing certain interface. Here is the sample which implements IWriter Interface implicitly:

        public class DocumentWriter : IWriter
        {
            #region IWriter Members
            public void WrtiteTo()
            {
                // Some Logic here
            }
            #endregion
        }
    

    Implicit: On the other hand Explicit implementation are exposed by the use of interface itself itself. Here is the sample which implements IWriter Interface Explicitly:

        public class DocumentWriter : IWriter
        {
            #region IWriter Members
            void IWriter.WrtiteTo()
            {
                 // Some Logic here
            }
            #endregion
        }
    

    Implement System.Collections.IList Interface using C#

    Today we will look into the interface System.Collections.IList and some of the its methods. A very good example is for IList implementation in use any class Inheriting from IList or ArraList class itself. Following is ArrayList Signature as on MSDN.

    public class ArrayList : IList, ICollection, IEnumerable, ICloneable
    

    To start with, let’s derive a simple class from IList

    public class ListImpl : IList
    { 
     private object[] _contents = new object[8];
    }
    

    We need to implement all members defined in the IList interface and and the interfaces: ICollection and IEnumerable as IList derives from these interfaces. Following is CopyTo Method from ICollection interface.

            public void CopyTo(Array array, int index)
            {
                int j = index;
                for (int i = 0; i < Count; i++)
                {
                    array.SetValue(_contents[i], j);
                    j++;
                }
            }
    

    Following is GetEnumerator method from IEnumerable interface.

    public IEnumerator GetEnumerator()
    {
      // Refer to the IEnumerator documentation another sample
      return (_contents as IEnumerable).GetEnumerator();
    }
    

    Here are some other methods and properties

          private int _count;
          public int Count
          {
                get
                {
                    return _count;
                }
           }
    
           // Add an object to the List
           public int Add(object value)
           {
               if (_count < _contents.Length)
               {
                   _contents[_count] = value;
                   _count++;
    
                   return (_count - 1);
               }
               else
               {
                    return -1;
               }
           }
    
           // Clear the List
           public void Clear()
            {
                _count = 0;
            }
    
           // Check if the List contains some value
           public bool Contains(object value)
            {
                bool inList = false;
                for (int i = 0; i < Count; i++)
                {
                    if (_contents[i] == value)
                    {
                        inList = true;
                        break;
                    }
                }
                return inList;
            }
    
           // Check if List is fixed size
           public bool IsFixedSize
            {
                get
                {
                    return true;
                }
            }
    
            // Overriding ToString() Method
            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                foreach (object o in _contents)
                {
                    sb.Append(o.ToString());
                }
                return sb.ToString();
            }
    

    We need to provide implementation for following methods as per requirements.

    
            public int IndexOf(object value)
            {
                throw new NotImplementedException();
            }
    
            public void Insert(int index, object value)
            {
                throw new NotImplementedException();
            }
    
            public bool IsReadOnly
            {
                get { throw new NotImplementedException(); }
            }
    
            public void Remove(object value)
            {
                throw new NotImplementedException();
            }
    
            public void RemoveAt(int index)
            {
                throw new NotImplementedException();
            }
    
            public object this[int index]
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    throw new NotImplementedException();
                }
            }
    
            public bool IsSynchronized
            {
                get { throw new NotImplementedException(); }
            }
    
            public object SyncRoot
            {
                get { throw new NotImplementedException(); }
            }