개발자 성장일지

Part 인터페이스 - 2 본문

인프런/C# 프로그래밍 기초

Part 인터페이스 - 2

hdigimon 2023. 12. 4. 19:57

IComparable

  • 객체를 비교하기 위한 인터페이스
  • 두 객체를 비교

ex) Student a CompareTo(Student b)

  • a가 작은 경우 음수 반환
  • a 와 b 가 같은 경우0을 반환
  • a가 큰 경우 양수 반환

코드

더보기
namespace IComparableTest
{
    internal class Exam1_2
    {
        public void Run()
        {
            Student[] students = new Student[]
            {
                new Student("홍강", 87, 2),
                new Student("김영", 76, 1)

            };
            Array.Sort(students);
            foreach (Student st in students)
            {
                Console.WriteLine(st);
            }
        }
    }

     class Student : IComparable
    {
        public string Name { get; set; }
        public int Score { get; set; }
        public int Id { get; set; }

        public Student(string name, int score, int id)
        {
            Name = name;
            Score = score;
            Id = id;
        }
        public override string ToString()
        {
            return $"{Name}[{Id}][{Score}]";
        }

        public int CompareTo(object obj)
        {
            Student st = obj as Student;
            int ret = Score - st.Score;
            return ret;
        }
    }
}

 

IDisposable

  • 객체의 자원을 해제하는 규격
    • Dispose() : 리소스 해제를 명시적으로 수행하기 위해 사용되는 메서드

코드

더보기
namespace IComparableTest
{
    internal class Exam1_2
    {
        public void Run()
        {
            Student[] students = new Student[]
            {
                new Student("홍강", 87, 2),
                new Student("김영", 76, 1)

            };
            Array.Sort(students);
            foreach (Student st in students)
            {
                Console.WriteLine(st);
            }
            FileStream stream = new FileStream(@"d:\temp\aa.txt", FileMode.Open);
            using(StreamReader reader = new StreamReader(stream))
            {
                string buffer;
                while ((buffer = reader.ReadLine()) != null)
                {

                }
            }
        }
    }

     class Student : IComparable, IDisposable
    {
        public string Name { get; set; }
        public int Score { get; set; }
        public int Id { get; set; }

        public Student(string name, int score, int id)
        {
            Name = name;
            Score = score;
            Id = id;
        }
        public override string ToString()
        {
            return $"{Name}[{Id}][{Score}]";
        }

        public int CompareTo(object obj)
        {
            Student st = obj as Student;
            int ret = Score - st.Score;
            return ret;
        }

        ~ Student()
        {
            Dispose(false);
        }
        bool disposed; // false
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose(bool disposing)
        {
            if (disposed) return;
            if (disposing)
            {
                // C#에서 사용하는 IDisposable을 구현한 객체들 정리

            }
            else
            {
                //.NET Framework 에서 관리되지 않는 자원들 정리

            }
            disposed = true;
        }

    }
}