Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace consoleapplication2 {class program {static void main (string [] ARGs) {money M = new money (); M. amount = 30.00 m; console. writeline (M. tostring (); // output $: 30.00 // use the extension method M. addtomoney (1.00 m); console. writeline (M. tostring (); // output $: 31.00 console. readkey () ;}} public class money {private decimal amount; Public decimal amount {set {amount = value ;}get {return amount ;}} public override string tostring () // override the tostring method {return "$:" + amount. tostring () ;}/// rules of the Extension Method public static class moneyextension {// This Money M extension method, tell the compiler that this method is part of the money type public static void addtomoney (this money M, decimal d) {M. amount + = D ;}}}
This article from the "programmer's home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1436085