Skip to main content
Engineering LibreTexts

8.5: C# Examples

  • Page ID
    10714
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Objects

    // This program creates instances of the Temperature class to convert Celsius 
    // and Fahrenheit temperatures.
    //
    // References:
    //     https://www.mathsisfun.com/temperature-conversion.html
    //     https://en.wikibooks.org/wiki/C_Sharp_Programming
    
    using System;
    
    public class Objects  
    {
        public static void Main(String[] args)
        {
            Temperature temp1 = new Temperature(celsius: 0);
            Console.WriteLine("temp1.Celsius = " + temp1.Celsius.ToString());
            Console.WriteLine("temp1.Fahrenheit = " + temp1.Fahrenheit.ToString());
            Console.WriteLine("");
        
            temp1.Celsius = 100;
            Console.WriteLine("temp1.Celsius = " + temp1.Celsius.ToString());
            Console.WriteLine("temp1.Fahrenheit = " + temp1.Fahrenheit.ToString());
            Console.WriteLine("");
            
            Temperature temp2 = new Temperature(fahrenheit: 0);
            Console.WriteLine("temp2.Fahrenheit = " + temp2.Fahrenheit.ToString());
            Console.WriteLine("temp2.Celsius = " + temp2.Celsius.ToString());
            Console.WriteLine("");
        
            temp2.Fahrenheit = 100;
            Console.WriteLine("temp2.Fahrenheit = " + temp2.Fahrenheit.ToString());
            Console.WriteLine("temp2.Celsius = " + temp2.Celsius.ToString());
        }
    }
    
    // This class converts temperature between Celsius and Fahrenheit.
    // It may be used by assigning a value to either Celsius or Fahrenheit 
    // and then retrieving the other value, or by calling the ToCelsius or
    // ToFahrenheit methods directly.
    
    public class Temperature
    {
        double _celsius;
        double _fahrenheit;
    
        public double Celsius
        {
            get
            {
                return _celsius;
            }
            
            set
            {
                _celsius = value;
                _fahrenheit = ToFahrenheit(value);
            }
        }
        
        public double Fahrenheit
        {
            get
            {
                return _fahrenheit;
            }
            
            set
            {
                _fahrenheit = value;
                _celsius = ToCelsius(value);
            }
        }
        
        public Temperature(double? celsius = null, double? fahrenheit = null)
        {
            if (celsius.HasValue)
            {
                this.Celsius = Convert.ToDouble(celsius);
            }
            
            if (fahrenheit.HasValue)
            {
                this.Fahrenheit = Convert.ToDouble(fahrenheit);
            }
        }
    
        public double ToCelsius(double fahrenheit)
        {
            return (fahrenheit - 32) * 5 / 9;
        }
        
        public double ToFahrenheit(double celsius)
        {
            return celsius * 9 / 5 + 32;
        }
    }
    

    Output

    temp1.Celsius = 0
    temp1.Fahrenheit = 32
    
    temp1.Celsius = 100
    temp1.Fahrenheit = 212
    
    temp2.Fahrenheit = 0
    temp2.Celsius = -17.7777777777778
    
    temp2.Fahrenheit = 100
    temp2.Celsius = 37.7777777777778
    

    References

    • Wikiversity: Computer Programming

    8.5: C# Examples is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?