This is my first post on C#. Funny why I cannot create “C#” category, have to use “C Sharp” instead. Wells…

Following up on my previous discussion about C++ Mixins, I went on to see if C# was able to do it as well. I then realized that this capability has been sitting in my computer for ages just waiting for today to be discovered by myself!

Incidentally, mixins could be implemented using Interfaces and Extension Methods introduced in C# 3.0 which is the version of VS2008 i am currently using. Furthermore, it seems that the implementation in C# is even more powerful.

Let’s first take a look at mixins in C#.

class Program
{
    static void Main(string[] args)
    {
        // our Son can do all the daily stuff a happy child can do.
        Son son = new Son();
        son.Talk();
        son.Eat();
        son.Walk();
        son.Kick();
        son.Jump();
    }
}

// We demostrate in C# where a class can derive from a parent class
// and also from multiple Interfaces.
// this multi-interface is the fundamentally how mixin is possible.
public class Father { }
public class Son : Father, IMouth, ILeg { }
// by implementing the IMouth and ILeg interfaces,
// our Son gains the respective abilities
// through extension methods below

// The mouth interface and extension method
public interface IMouth {}
public static class Mouth
{
    public static void Talk(this IMouth mouth)
    {
        Console.WriteLine("Talking");
    }

    public static void Eat(this IMouth mouth)
    {
        Console.WriteLine("Eating");
    }
}

// The leg interface and extension method
public interface ILeg {}
public static class Leg
{
    public static void Walk(this ILeg leg)
    {
        Console.WriteLine("Walking");
    }

    public static void Kick(this ILeg leg)
    {
        Console.WriteLine("Kicking");
    }

    public static void Jump(this ILeg leg)
    {
        Console.WriteLine("Jumping");
    }
}

Unlike C++, C# cannot take an unknown class type as base class, hence Extension Methods seems to be the only way we know so far. However, the C# implementation offers some unique benefits that C++ does not have.

In our previous C++ examples in here, notice that our mixin classes have no knowledge of its base class, hence, they have no way to access base methods and variables. Basically, the Mouth and Leg in C++ always act on their own without control from the Son.

In C#, things get a little bit better. Due to the fact that our Son implements the IMouth and ILeg interfaces, the Extension Method can interface with the Son. Take for example the following code.

class Program
{
    static void Main(string[] args)
    {
        Son son = new Son();
        son.Eat(); // Chewing
        son.Talk(); // I Like Minced Beef
    }
}

public class Father { }
public class Son : Father, IMouth
{
    // Son implements the IMouth interface to give some access to the Mouth mixin
    private String mFood;
    public Son() { mFood = "Beef"; }
    public String food { get { return mFood; } set { mFood = value; } }
}

// the IMouth interface enforces the requirement for the food property
public interface IMouth
{
    String food { get; set; }
}
public static class Mouth
{
    // the food property is used and the value is provided
    // by the implementing class
    public static void Eat(this IMouth mouth)
    {
        Console.WriteLine("Chewing");
        mouth.food = String.Concat("Minced ", mouth.food);
    }

    public static void Talk(this IMouth mouth)
    {
        Console.WriteLine("I Like " + mouth.food);
    }
}

Now, not only our Son gets new abilities, our Son can also excercise control over its new abilities. “With great powers comes great responsibilities”!