Jump to content

Object-Oriented Programming/Methods/C Sharp

From Wikiversity

main.cs

[edit | edit source]
/// <summary>
/// This program demonstrates use of the Temperature class.
/// </summary>
/// <remarks>
/// References:
///     * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
///     * http://www.mathsisfun.com/temperature-conversion.html
///     * https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/how-to-use-the-xml-documentation-features
///     * https://blog.rsuter.com/best-practices-for-writing-xml-documentation-phrases-in-c/
/// </remarks>

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();
        Console.WriteLine(temperature.ToCelsius(98.6));
        Console.WriteLine(temperature.ToFahrenheit(37));
    }
}

temperature.cs

[edit | edit source]
/// <summary>
/// Temperature converter. Supports Fahrenheit and Celius temperatures.
/// </summary>
/// <remarks>
/// Examples:
///    var temperature = new Temperature();
///    Console.WriteLine(temperature.ToCelsius(98.6));
///    Console.WriteLine(temperature.ToFahrenheit(37));
///
/// References:
///     * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
///     * http://www.mathsisfun.com/temperature-conversion.html
///     * https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/how-to-use-the-xml-documentation-features
///     * https://blog.rsuter.com/best-practices-for-writing-xml-documentation-phrases-in-c/
/// </remarks>

/// <summary>
/// Provides temperature conversion functions.
/// </summary>
public class Temperature
{
    /// <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 | edit source]

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 | edit source]