- 인터페이스는 클래스 또는 구조체에서 구현할 수 있는 기능에 대한 정의를 포함한다. 
- 클래스나 구조체는 인터페이스를 상속받아 인터페이스의 기능을 정의해준다. 
- 인터페이스를 상속받은 클래스나 구조체는 인터페이스의 모든 메소드를 구현해야 한다. 
IEquatable 인터페이스
interface IEquatable<T>
{
    bool Equals(T obj);
}인터페이스를 상속받은 클래스
public class Car : IEquatable<Car>
{
    public string Model {get; set;}
    public int Year {get; set;}
    public bool Equals(Car car)
    {
        if(this.Model == car.Model && this.Year == car.Year){
            return true;
        }
        return false;
    }
}- Equals 메소드를 구현해야한다. 
인터페이스를 상속받은 구조체
struct Vector3 : IEquatable<Vector3>
{
    public float x, y, z;
    public Vector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public bool Equals(Vector3 v)
    {
        if(this.x == v.x && this.y == v.y && this.z == v.z)
        {
            return true;
        }
        return false;
    }
}- 마찬가지로 Equals 메소드를 구현해야 한다. 
테스트
class MainApp
{
    static void Main()
    {
        Car car1 = new Car();
        car1.Model = "BMW";
        car1.Year = 1992;
        Car car2 = new Car();
        car2.Model = "BMW";
        car2.Year = 1992;
        Console.WriteLine(car1.Equals(car2));
        car2.Year = 1993;
        Console.WriteLine(car1.Equals(car2));
        Vector3 pos1 = new Vector3(1f, 0f, 0f);
        Vector3 pos2 = new Vector3(1f, 0f, 0f);
        Console.WriteLine(pos1.Equals(pos2));
    }
}결과
True
False
True
참조
- 뇌를 자극하는 C# 5.0 프로그래밍, 박상현, 한빛미디어 
- https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/interfaces/ 
'아카이빙 > C#' 카테고리의 다른 글
| [C#] 인터페이스 다중 상속 (0) | 2018.06.18 | 
|---|---|
| [C#] 인터페이스를 상속하는 인터페이스 (0) | 2018.06.18 | 
| [C#] 구조체 (0) | 2018.06.18 | 
| [C#] 확장 메소드 (Extension Method) (0) | 2018.06.18 | 
| [C#] 오버라이딩 (0) | 2018.06.18 |