using System.Collections.Generic;
public class Temperature : IComparable<Temperature>, IEquatable<Temperature>
public int CompareTo(Temperature other)
if (other == null) return 1;
return m_value.CompareTo(other.m_value);
public static bool operator > (Temperature operand1, Temperature operand2)
return operand1.CompareTo(operand2) > 0;
public static bool operator < (Temperature operand1, Temperature operand2)
return operand1.CompareTo(operand2) < 0;
public static bool operator >= (Temperature operand1, Temperature operand2)
return operand1.CompareTo(operand2) >= 0;
public static bool operator <= (Temperature operand1, Temperature operand2)
return operand1.CompareTo(operand2) <= 0;
public bool Equals(Temperature other)
public override int GetHashCode()
return Guid.NewGuid().GetHashCode();
protected double m_value = 0.0;
throw new ArgumentException("Temperature cannot be less than absolute zero.");
public Temperature(double kelvins)
public static void WriteArray(string tag, string[] arr) {
Console.WriteLine("--------------------------------------------------------------------------------------");
Console.WriteLine("--------------------------------------------------------------------------------------");
for (var i=0;i<arr.Length;i++) {
Console.WriteLine($"{i.ToString()} :{arr[i]}");
catch (System.NullReferenceException ex) {
Console.WriteLine(ex.Message);
public static void Main()
SortedList<Temperature, string> temps =
new SortedList<Temperature, string>();
temps.Add(new Temperature(2017.15), "Boiling point of Lead");
temps.Add(new Temperature(0), "Absolute zero");
temps.Add(new Temperature(273.15), "Freezing point of water");
temps.Add(new Temperature(5100.15), "Boiling point of Carbon");
temps.Add(new Temperature(373.15), "Boiling point of water");
temps.Add(new Temperature(600.65), "Melting point of Lead");
var temps1 = temps.Select(s=>s).ToList();
var kvp1 = KeyValuePair.Create<Temperature, string >(new Temperature(5100.15), "Dup");
foreach( KeyValuePair<Temperature, string> kvp in temps1 )
Console.WriteLine("{0} is {1} degrees Celsius.", kvp.Value, kvp.Key.Celsius);
var dist1 = temps1.Select(s=>s.Key).Select(s=>s.Kelvin.ToString()).ToArray();
WriteArray("Non-Distinct in Pocos implements IEquatable<Person>", dist1);
var dist = temps1.Select(s=>s.Key).Distinct().Select(s=>s.Kelvin.ToString()).ToArray();
WriteArray("Distinct in Pocos implements IEquatable<Person>", dist);