Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement mixins in C#?

I've heard that it's possible with extension methods, but I can't quite figure it out myself. I'd like to see a specific example if possible.

Thanks!

like image 798
Stewart Johnson Avatar asked Nov 01 '08 05:11

Stewart Johnson


People also ask

How do you implement mixin?

The simplest way to implement a mixin in JavaScript is to make an object with useful methods, so that we can easily merge them into a prototype of any class.

What are Mixins in C#?

A mixin is a class which adds functionality to other classes but which cannot itself be instantiated. Mixins are useful in object oriented languages which only support single inheritance as a sort of 'half way house' between single and multiple inheritance.

Is mixin same as interface?

Mixins are hailed as interfaces with behavioral reuse, more flexible interfaces, and more powerful interfaces. You will notice all these have the term interface in them, referring to the Java and C# keyword. Mixins are not interfaces. They are multiple inheritance.

What is the difference between a mixin and inheritance C#?

Mixins are sometimes described as being "included" rather than "inherited". In short, the key difference from an inheritance is that mix-ins does NOT need to have a "is-a" relationship like in inheritance. From the implementation point of view, you can think it as an interface with implementations.


1 Answers

It really depends on what you mean by "mixin" - everyone seems to have a slightly different idea. The kind of mixin I'd like to see (but which isn't available in C#) is making implementation-through-composition simple:

public class Mixin : ISomeInterface {     private SomeImplementation impl implements ISomeInterface;      public void OneMethod()     {         // Specialise just this method     } } 

The compiler would implement ISomeInterface just by proxying every member to "impl" unless there was another implementation in the class directly.

None of this is possible at the moment though :)

like image 146
Jon Skeet Avatar answered Sep 18 '22 15:09

Jon Skeet