Programming Stuff : .Net Examples : Using Interfaces and Extensions For Multiple Inheritance

We were interviewing someone at work the other day, and we asked them about empty interfaces. I had a thought about using extension methods on an interface and decided to try it. Could I use an interface, write an extension method that extends the interface instead of a class, and then use that extension method in every class that uses the interface instead of having to implement that method in each class I want it to be available on? This is similar to how multiple inheritance is done in Perl. I tested this out and it worked perfectly.

using System;
using System.Collections.Generic;
using System.Text;

namespace InterfaceExtensionsOneFileExample
{
    /// <summary>
    /// ILogable. Empty interface to abuse.
    /// </summary>
    public interface ILogable
    {
        // All kids love log.
    }

    /// <summary>
    /// NeedsToBeLogged. A concrete class that implements ILogable.
    /// </summary>
    public class NeedsToBeLogged : ILogable
    {
        // Look ma, no methods!
    }

    /// <summary>
    /// NeedsToBeLoggedToo. Another concrete class that implements ILogable.
    /// </summary>
    public class NeedsToBeLoggedToo : ILogable
    {
        // No methods up my sleeve.
    }

    /// <summary>
    /// ExtendILog. A class where extension methods can hang out.
    /// </summary>
    public static class ExtendILog
    {
        /// <summary>
        /// WriteLog. Outputs a line. Will be available to any class that implements the ILogable interface.
        /// </summary>
        /// <param name="logable">The object to be extended.</param>
        public static void WriteLog(this ILogable logable)
        {
            Console.WriteLine("You got logged '{0}'!", logable.GetType().ToString());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Create a list to put some items in that I would like to log.
            List<ILogable> thingsThatNeedLogged = new List<ILogable>();
            thingsThatNeedLogged.Add(new NeedsToBeLogged());
            thingsThatNeedLogged.Add(new NeedsToBeLoggedToo());

            // Iterate and log.
            thingsThatNeedLogged.ForEach(thing => thing.WriteLog());

            // This program will output the following:
            // You got logged 'InterfaceExtensionsOneFileExample.NeedsToBeLogged'!
            // You got logged 'InterfaceExtensionsOneFileExample.NeedsToBeLoggedToo'!
            // Press any key to continue . . .
        }
    }
}