Mixin can be implemented in C# 3.0 using an extension method.
Functionality can be added by extending
the interface implemented by the class.
Consider the following example.
1: using System;
2: 3: namespace Mixin
4: 5: { 6: 7: class Person : IPerson
8: 9: { 10: 11: private int _age;
12: 13: private string _name;
14: 15: #region IPerson Members
16: 17: public int age
18: 19: { 20: a 21: get 22: 23: { 24: 25: return _age;
26: 27: } 28: 29: set 30: 31: { 32: 33: if (value > 0)
34: 35: { 36: 37: _age = value;
38: 39: } 40: 41: 42: } 43: 44: } 45: 46: 47: public string name
48: 49: { 50: 51: get 52: 53: { 54: 55: return _name;
56: 57: } 58: 59: set 60: 61: { 62: 63: if (!string.IsNullOrEmpty(value))
64: 65: { 66: 67: _name = value;
68: 69: } 70: 71: else
72: 73: { 74: 75: _name = "Unknown";
76: 77: } 78: 79: } 80: 81: } 82: 83: #endregion
84: 85: } 86: 87: interface IPerson
88: 89: { 90: 91: int age { get; set; }
92: 93: String name { get; set; } 94: 95: } 96: 97: static class ExtendIPerson
98: 99: { 100: 101: public static bool CanDrive(this IPerson testPerson, bool isDriver)
102: 103: { 104: 105: if (isDriver)
106: 107: return true;
108: 109: return false;
110: 111: } 112: 113: } 114: 115: class Program
116: 117: { 118: 119: static void Main(string[] args)
120: 121: { 122: 123: IPerson ip = new Person();
124: 125: 126: ip.name = "ABC";
127: 128: ip.age = 21; 129: 130: if(ip.CanDrive(true))
131: 132: { 133: 134: Console.WriteLine(ip.name +" can drive");
135: 136: } 137: 138: Console.ReadLine(); 139: 140: } 141: 142: } 143: 144: } 145: In this example, we implemented mixin by extending the IPerson interface
[line 87] using CanDrive extension method defined
in the ExtendIPerson static class[line 101].
Notice the code on line 130 ip.CanDrive - an example of C# mixin.
Another explanation of the mixin can be found at
http://mikehadlow.blogspot.com/2008/05/interface-extension-methods-mixin.html

Join the conversation! Your thoughts help the community grow.