Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)?

share|improve this question
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 '12 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 '12 at 12:37
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? –  Jörg W Mittag Feb 19 '12 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 '12 at 7:09
add comment

2 Answers

up vote 3 down vote accepted

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?

share|improve this answer
"mixin" is a pretty generic concept which isn't specific to any modern language. –  DeadMG Feb 19 '12 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 '12 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 '12 at 6:42
add comment

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.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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