C # examples of delegation in advanced programming.

Source: Internet
Author: User

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system. text;

 

Namespace consoleapplication1

{

Public class currency

{

Public uint dollars;

Public ushort cents;

 

Public currency ()

{

 

}

Public currency (uint dollars, ushort cents)

{

This. Dollars = dollars;

This. cents = cents;

 

}

Public override string tostring ()

{

Return string. Format ("{0: c}. {1,-2: 00}", dollars, cents );

}

Public static string getcurrencyuint ()

{

Return "RMB ";

}

Public static explicit operator currency (float value)

{

Checked

{

Uint dollars = (uint) value;

Ushort cents = (ushort) (value-dollars) * 100 );

Return new currency (dollars, cents );

}

}

Public Static Implicit operator float (currency value)

{

Return Value. dollars + value. cents/100366f;

 

}

Public Static Implicit operator currency (uint value)

{

Return new currency (value, 0 );

}

Public Static Implicit operator uint (currency value)

{

Return Value. dollars;

}

}

Delegate string getastring ();

Class Program

{

Static void main (string [] ARGs)

{

Int x = 40;

Getastring firststringmethod = x. tostring;

Console. writeline ("string is {0}", firststringmethod ());

 

Currency balance = new currency (34, 50 );

 

Firststringmethod = balance. tostring;

 

Console. writeline ("string is {0}", firststringmethod ());

 

Firststringmethod = new getastring (currency. getcurrencyuint );

Console. writeline ("string is {0}", firststringmethod ());

}

}

}

 

//

/// * The following is the running result. String is 40 string is ¥34.50 string is RMB. Press any key to continue ...*/

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.