-
03/24 SortedList, IComparable 연습C#/수업내용 2021. 3. 24. 15:16
Program.cs
using System; namespace Study07 { class Program { static void Main(string[] args) { Console.WriteLine("Main"); new App(); } } }
App.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace Study07 { public class App { //생성자 public App() { Console.WriteLine("App"); //컬렉션 인스턴스화 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"); Console.WriteLine(temps.Count); foreach (var pair in temps) { Console.WriteLine("kelvin: {0}, desc: {1}", pair.Key.Kelvin, pair.Value); } var t1 = new Temperature(600.65); var t2 = new Temperature(5100.15); var result = t1.CompareTo(t2); Console.WriteLine(result); } } }
Temperature.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study07 { public class Temperature : IComparable<Temperature> { public double Kelvin { get { return m_value; } set { if (value < 0) throw new ArgumentException("Temperature cannot be less than absolute zero."); else m_value = value; } } public double m_value; public Temperature(double kelvins) { Kelvin = kelvins; } public int CompareTo(Temperature other) { if (other == null) return 1; return this.Kelvin.CompareTo(other.Kelvin); } } }
'C# > 수업내용' 카테고리의 다른 글
03/24 직렬화, JSON, 파일시스템 연습 (0) 2021.03.24 03/24 event연습 (0) 2021.03.24 03/24 callback 연습 (0) 2021.03.24 03/24 델리게이트 연습 (0) 2021.03.24 03/24 Event연습 (0) 2021.03.24