사용자정의 자료형이나 클래스의 연산자를 재정의하여 여러 의미로 사용
namespace _2024_07_25_7
{
internal class Program
{
public struct Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public static Point operator +(Point a, Point b)
{
return new Point (a.x + b.x, a.y + b.y);
}
public static Point operator -(Point a, Point b)
{
return new Point (a.x - b.x, a.y - b.y);
}
}
static void Main(string[] args)
{
Point point1 = new Point(1,2);
Point point2 = new Point(2,3);
// (1,2) + (2,3) == (3,5)
Point sum = point1 + point2;
Point minus = point1 - point2;
Console.WriteLine($"{sum.x} {sum.y}");
Console.WriteLine($"{minus.x} {minus.y}");
}
}
}
'C# (수업 정리)' 카테고리의 다른 글
C#: 리스트 (0) | 2024.07.26 |
---|---|
C#: 자료구조 (0) | 2024.07.26 |
C#: Getter / Setter (0) | 2024.07.25 |
C#: 확장 메서드 (0) | 2024.07.25 |
C#: 델리게이트 지정자 (0) | 2024.07.25 |