Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Util.Independent
- {
- /// <summary>
- /// Type that represents temperature.
- /// </summary>
- public class Temperature: IComparable<Temperature>
- {
- public override int GetHashCode()
- {
- return kelvin.GetHashCode();
- }
- private readonly float kelvin;
- public float Kelvin => kelvin;
- public float Celsius => kelvin - 273.15f;
- public float Fahrenheit => (kelvin - 273.15f) * 9 / 5 + 32;
- #region Constructors
- private Temperature(float value)
- {
- kelvin = value;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Temperature"/> class using a float in Kelvin.
- /// </summary>
- /// <param name="value"></param>
- /// <returns>Temperature object</returns>
- public static Temperature FromKelvin(float value)
- {
- return new Temperature(value);
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Temperature"/> class using a float in Celsius.
- /// </summary>
- /// <param name="value"></param>
- /// <returns>Temperature object</returns>
- public static Temperature FromCelsius(float value)
- {
- return new Temperature(value + 273.15f);
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Temperature"/> class using a float in Fahrenheit.
- /// </summary>
- /// <param name="value"></param>
- /// <returns>Temperature object</returns>
- public static Temperature FromFahrenheit(float value)
- {
- return new Temperature((value + 459.67f) * 5 / 9);
- }
- #endregion
- #region operators
- public static Temperature operator +(Temperature a, Temperature b)
- {
- return new Temperature(a.kelvin + b.kelvin);
- }
- public static Temperature operator -(Temperature a, Temperature b)
- {
- return new Temperature(a.kelvin - b.kelvin);
- }
- public static Temperature operator *(Temperature a, float b)
- {
- return new Temperature(a.kelvin * b);
- }
- public static Temperature operator /(Temperature a, float b)
- {
- return new Temperature(a.kelvin / b);
- }
- public static Temperature operator *(float a, Temperature b)
- {
- return new Temperature(a * b.kelvin);
- }
- public static Temperature operator /(float a, Temperature b)
- {
- return new Temperature(a / b.kelvin);
- }
- public static bool operator ==(Temperature a, Temperature b)
- {
- return a!.Equals(b);
- }
- public static bool operator !=(Temperature a, Temperature b)
- {
- return a!.Equals(b) == false;
- }
- public static bool operator <(Temperature a, Temperature b)
- {
- return a.kelvin < b.kelvin;
- }
- public static bool operator >(Temperature a, Temperature b)
- {
- return a.kelvin > b.kelvin;
- }
- public int CompareTo(Temperature other)
- {
- if (ReferenceEquals(this, other)) return 0;
- if (ReferenceEquals(null, other)) return 1;
- return kelvin.CompareTo(other.kelvin);
- }
- private bool Equals(Temperature other)
- {
- return kelvin.Equals(other.kelvin);
- }
- public override bool Equals(object obj)
- {
- if (ReferenceEquals(null, obj)) return false;
- if (ReferenceEquals(this, obj)) return true;
- if (obj.GetType() != GetType()) return false;
- return Equals((Temperature)obj);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement