Object-Oriented Programming/Properties/C Sharp

main.cs

edit
/// <summary>
/// This program demonstrates use of the Temperature class.
/// </summary>

using System;

/// <summary>
/// Provides the main program code.
/// </summary>
class Program {
    /// <summary>
    /// Runs the main program.
    /// </summary>
    public static void Main (string[] args) {
        var temperature = new Temperature(celsius: 37);
        Console.WriteLine(temperature.Fahrenheit);
        
        temperature = new Temperature(fahrenheit: 98.6);
        Console.WriteLine(temperature.Celsius);
        
        temperature = new Temperature();
        temperature.Celsius = 37;
        Console.WriteLine(temperature.Fahrenheit);
        
        temperature = new Temperature();
        temperature.Fahrenheit = 98.6;
        Console.WriteLine(temperature.Celsius);
        
        temperature = new Temperature();
        Console.WriteLine(temperature.ToCelsius(98.6));
        Console.WriteLine(temperature.ToFahrenheit(37)); 
    }
}

temperature.cs

edit
/// <summary>
/// Temperature converter. Provides temperature conversion functions. Supports Fahrenheit and Celsius temperatures.
/// </summary>
/// <remarks>
/// Examples:
///    var temperature = new Temperature(celsius: 37);
///    Console.WriteLine(temperature.Fahrenheit);
///    
///    temperature = new Temperature(fahrenheit: 98.6);
///    Console.WriteLine(temperature.Celsius);
///    
///    temperature = new Temperature();
///    temperature.Celsius = 37;
///    Console.WriteLine(temperature.Fahrenheit);
///    
///    temperature = new Temperature();
///    temperature.Fahrenheit = 98.6;
///    Console.WriteLine(temperature.Celsius);
///    
///    temperature = new Temperature();
///    Console.WriteLine(temperature.ToCelsius(98.6));
///    Console.WriteLine(temperature.ToFahrenheit(37)); 
///
/// References:
///   https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments
///   https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types
///   https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
/// </remarks>

public class Temperature
{
    private double _celsius;
    private double _fahrenheit;

    /// <summary>
    /// Creates a Temperature object.
    /// </summary>
    public Temperature(double? celsius=null, double? fahrenheit=null)
    {
        if (celsius != null)
        {
            Celsius = (double)celsius;
        }

        if (fahrenheit != null)
        {
            Fahrenheit = (double)fahrenheit;
        }
    }

    /// <summary>
    /// Gets and sets Celsius temperature.
    /// </summary>
    public double Celsius
    {
        get
        {
            return _celsius;
        }

        set
        {
            _celsius = value;
            _fahrenheit = ToFahrenheit(value);
        }
    }

    /// <summary>
    /// Gets and sets Fahrenheit temperature.
    /// </summary>
    public double Fahrenheit
    {
        get
        {
            return _fahrenheit;
        }

        set
        {
            _fahrenheit = value;
            _celsius = ToCelsius(value);
        }
    }

    /// <summary>
    /// Converts Fahrenheit temperature to Celsius.
    /// </summary>
    public double ToCelsius(double fahrenheit)
    {
        return (fahrenheit - 32) * 5 / 9;
    }

    /// <summary>
    /// Converts Celsius temperature to Fahrenheit.
    /// </summary>
    public double ToFahrenheit(double celsius)
    {
        return celsius * 9 / 5 + 32;
    }
}

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own C Sharp compiler / interpreter / IDE.

See Also

edit