I want to present the concept of mixins and their implementation in .NET.

The way they work in .NET is you add extension methods on an interface and decorate classes with said interface.

What would be the best examples for this pattern (other than linq)?

  • Just to clarify are you looking for the abstract base class sample or is it something else? Term mixin seems to be unfamiliar at least in .NET.
    – oleksii
    Feb 19, 2012 at 10:34
  • No, not abstract base classes. Mixins allow for a form of multiple inheritance. Say you have a bat. It's a specialization of the mammal class. But it can also fly. A mixin would allow you to implement the flight logic in one single place, and decorate all the classes you want (e.g. bats, owles, bugs, etc.)
    – linkerro
    Feb 19, 2012 at 12:37
  • 2
    A mixin is a class that is parameterized by its superclass. I don't really see what extension methods have to do with that. Can you explain? Feb 19, 2012 at 14:51
  • There are various ways of implementing the mixin pattern, and it takes different forms in different programming languages (the pattern being very loosely defined). Basically it's a way of adding utility code to a class by sidestepping "normal" inheritance, since you need that for something else entirely.
    – linkerro
    Feb 20, 2012 at 7:09

3 Answers 3


As far as I know, "mixin" is a Ruby concept for handling multiple inheritance limitations, and does not have a built-in comparable way of doing it in C#, VB.NET, or Java.

Your suggestion of adding extension methods to an interface is an interesting one, and would provide much of the benefit of a mixin. I've never actually seen anyone do it that way before, so I'll have to put that in my bag of tricks...

As far as an example, I think the one you put in your comment above isn't a bad one; demonstrating that a Bat is a mammal that can fly. Without making your own contrived example like that, I can't think of anyplace where this just "occurs" in any of the core .NET libraries, other than Linq (sort of, though even IEnumerable requires that you define the GetEnumerator method in the implementing class, so it isn't a purely "empty" interface with only extension methods.)

Is there a problem with presenting your own contrived example?

  • 2
    "mixin" is a pretty generic concept which isn't specific to any modern language.
    – DeadMG
    Feb 19, 2012 at 16:19
  • Agreed the concept of a mixin isn't language specific. I just assumed (possibly falsely) that the original question was talking about "ruby style mixins" since it is the most "modern" language that i know of that supports them.
    – rally25rs
    Feb 19, 2012 at 16:29
  • Any language with both type polymorphism and inheritance supports mixins- even Java. It's been found in C++ since 1995 at least.
    – DeadMG
    Feb 20, 2012 at 6:42
  • I hope those are scare quotes! Modern isn't what comes to mind when talking about a language that isn't just dynamically typed, but also allows any type to be monkey-patched.
    – Doval
    May 9, 2014 at 13:42
  • 1
    @Doval I can see why you don't like those features, but I can't imagine it's because they're not "modern". Dynamic typing (as implemented in Ruby) and monkey patching aren't really what come to mind when I think of "old" languages.
    – KChaloux
    May 9, 2014 at 14:28

Don't know if it's the best example, but I've got an example of using extension methods to approximate mixins on my blog: http://horsdal.blogspot.com/2009/05/dci-in-c.html?m=0

Please note that you don't really get mixins by doing this, just something that sort of like mixins from the client codes perspective.

  • "Please note that you don't really get mixins by doing this, just something that sort of like mixins from the client codes perspective." What's the difference, and perhaps more importantly, what's the definition of mixins you're using? Everyone seems to have a different one.
    – Doval
    May 9, 2014 at 13:53
  • 1
    @Doval: I would imagine that the difference is that a true mixin will have access to a class's internals. Extension methods do not. Extension methods are just syntactic sugar for static method calls, so all they have access to is a class's external API. May 9, 2014 at 14:13
  • @RobertHarvey Interesting. When you say "a class's internals", do you mean something internal to the mixin, or the internals of the class being decorated?
    – Doval
    May 9, 2014 at 14:16
  • @Doval: Extension methods won't have access to anything that a class's public methods don't already provide. May 9, 2014 at 14:19
  • 1
    @Doval: I guess what I'm trying to say is that anything you can do with an extension method, you can already do with the public API of an ordinary class anyway, albeit with different syntax. Not directly answering your question, but without access to class internals I see any interaction between classes via the external API as ordinary composition, unless I'm missing something blindingly obvious about mixins. May 9, 2014 at 14:25

I've been working on an open source Mixin framework for C# pMixins. It leverages partial classes and code generators to wire in the Mixin class into the Target:

//Mixin - Class that contains members that should be injected into other classes.
public class Mixin
   // This method should be in several class
   public void Method(){ }

//Target (Note: That it is partial) - Add members from Mixin
[pMixn(Target = typeof(Mixin)]
public partial class Target{}

//Example of using Target
public class Consumer
    public void Example()
        var target = new Target();

        // can call mixed in method

        // can implicitly convert Target to Mixin
        Mixin m = new Target();

Not the answer you're looking for? Browse other questions tagged or ask your own question.