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)