Rewrite tostring and implement Extension Method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.