[C#] [] 연산자 커스텀

0시0분·2024년 8월 13일
0

C#

목록 보기
9/9

맵 정보를 데이터화 하면서 2차원 배열로 되어있던 구조를 리스트로 변경했는데
더이상 (x, y) 좌표로 정보를 가지고 올 수 없게 되자 불편함이 생겼다.

고민하다 예전처럼 [] 연산자를 사용하는 것이 제일 편할 것 같아서 커스텀하기로 했다.

public class MapTileList
{
    public List<MapTile> tiles = new List<MapTile>();

	...
	
    // 🔽 [] 연산자 커스텀
    public MapTile this[int x, int y]
    {
        get => tiles.Find(tile => tile.tilePos == new Vector2Int(x, y));
    }
}

이런식으로 사용할 수 있다.

MapTile startTile = mapInfos[startPos.x - bottomLeftPos.x, startPos.y - bottomLeftPos.y];

👀 참고
https://stackoverflow.com/questions/424669/how-do-i-overload-the-operator-in-c-sharp

0개의 댓글