Switch 식을 이용한 사이즈 조절

호불호? 가 있을 수도 있는, 그런데 저는 아주 만족스러운 코드 스타일입니다.

        public void Resize(ActionCursorKind actionCursor, Point p)
        {
            Rect = actionCursor switch
            {
                ActionCursorKind.LeftResize => new(Rect.Left + p.X, Rect.Top, Rect.Width - p.X, Rect.Height),
                ActionCursorKind.LeftTopResize => new(Rect.Left + p.X, Rect.Top + p.Y, Rect.Width - p.X, Rect.Height - p.Y),
                ActionCursorKind.TopResize => new(Rect.Left, Rect.Top + p.Y, Rect.Width, Rect.Height - p.Y),
                ActionCursorKind.RightTopResize => new(Rect.Left, Rect.Top + p.Y, Rect.Width + p.X, Rect.Height - p.Y),
                ActionCursorKind.RightResize => new(Rect.Left, Rect.Top, Rect.Width + p.X, Rect.Height),
                ActionCursorKind.RightBottomResize => new(Rect.Left, Rect.Top, Rect.Width + p.X, Rect.Height + p.Y),
                ActionCursorKind.BottomResize => new(Rect.Left, Rect.Top, Rect.Width, Rect.Height + p.Y),
                ActionCursorKind.LeftBottomResize => new(Rect.Left + p.X, Rect.Top, Rect.Width - p.X, Rect.Height + p.Y),
                _ => Rect
            };
        }
3개의 좋아요

저도 패턴 매칭 상당히 좋아합니다.
보여주신것도 아주 좋은 작성법인거 같습니다.

1개의 좋아요

참고로, 기존 switch 문을 바꿀 때는 아무 생각하지 마시고 Visual Studio의 도움을 받으세요. ^^

Convert switch statement to switch expression
; Convert switch statement to switch expression - Visual Studio (Windows) | Microsoft Learn

4개의 좋아요