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
        }