C # operator overloading and conversion operator instances (two structs)

Source: Internet
Author: User

structcelsius{Private floatdegrees;  Public floatDegrees {Get{return  This. degrees;} } PublicCelsius (floattemp) { This. degrees =temp; Console.WriteLine ("This is the structure of the Celsius temperature! ");} Public StaticCelsiusoperator+ (Celsius x, Celsius y)//Overloaded + operators{return NewCelsius (X.degrees +y.degrees);} Public StaticCelsiusoperator-(Celsius x, Celsius y)//Overloaded-Operator{return NewCelsius (X.degrees-y.degrees);}/// <summary>///implicitly converts Celsius to float/// </summary>/// <param name= "C" >Celsius type parameter</param>/// <returns>Float type return value</returns> Public Static Implicit operator float(Celsius c) {returnc.degrees;}/// <summary>///The implicit conversion of float to Celsius/// </summary>/// <param name= "F" >Float type parameter</param>/// <returns>Celsius type return value</returns> Public Static Implicit operatorCelsius (floatf) {Celsius C=NewCelsius (f); returnC;}/// <summary>///conversion operator, which shows the conversion of the Celsius type to the Fahrenheit type/// </summary>/// <param name= "C" >Celsius type parameter</param>/// <returns>Fahrenheit type return value</returns>///  Public Static Explicit operatorFahrenheit (Celsius c) {floatFl= ((9.0f/5.0f) * C.degrees + +);//rules for converting Celsius types into FahrenheitFahrenheit F =NewFahrenheit (FL); returnF;} Public Override stringToString ()//overriding ToString{return  This. Degrees.tostring ();}} structfahrenheit{Private floatdegrees;  Public floatDegrees {Get{return  This. degrees;} } PublicFahrenheit (floattemp) { This. degrees =temp; Console.WriteLine ("This is the temperature of the Fahrenheit structure! ");} Public StaticFahrenheitoperator+(Fahrenheit x, Fahrenheit y) {return NewFahrenheit (X.degrees +y.degrees);} Public StaticFahrenheitoperator-(Fahrenheit x, Fahrenheit y) {return NewFahrenheit (X.degrees-y.degrees);} Public Static Implicit operator float(Fahrenheit c) {returnc.degrees;} Public Static Implicit operatorFahrenheit (floatf) {Fahrenheit FA=NewFahrenheit (f); returnFA;} Public Static Explicit operatorCelsius (Fahrenheit f) {floatFL = (5.0f/9.0f) * (F.degrees- +); Celsius C=NewCelsius (FL); returnC;} Public Override stringToString () {return  This. Degrees.tostring ();}} classprogram{Static voidMain (string[] args) {Fahrenheit F=NewFahrenheit (100f); Console.WriteLine ("{0} Fahrenheit = {1} Celsius", F.degrees, (Celsius) f); Celsius C= 32f;//implicitly converts float to CelsiusConsole.WriteLine ("{0} Celsius = {1} Fahrenheit", C.degrees, (Fahrenheit) c); Fahrenheit F2= f + (Fahrenheit) C;//Verifying the Fahrenheit + overloadsConsole.WriteLine ("{0} + {1} = {2} Fahernheit", F.degrees, (Fahrenheit) c,f2.  Degrees); Fahrenheit F3= 100f;//implicitly converts float to FahrenheitConsole.WriteLine ("{0} Fahrenheit", F3. Degrees);}}

C # operator overloading and conversion operator instances (two structs)

Related Article

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.