반응형
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Real Vectorism. 훨씬 더 입체적으로...

존재하는 줄도 몰랐던 문법. LinQ 본문

C# (based by Unity)

존재하는 줄도 몰랐던 문법. LinQ

grast 2020. 7. 3. 00:21
반응형

사용방법이며 용도까지 완전히 어이없지만 새롭고 웃긴 문법이 있다. 자바 개발자들에게는 전혀 생각지도 못한 C#의 새로운 문법인 LinQ이다. List 등의 배열객체에서 데이터를 필요한 조건에 따라 검색해서 선별하고자 할 경우에 사용하는 방법 중 하나이다.

 

0부터 7까지의 자연수가 있는 리스트에서 짝수만 추려 결과리스트로 옮겨담는 소스코드다. 5개 소스코드 모두 똑같은 결과이다. LinQ 예제는 4번째와 5번째이다.

// 아직 C#이 손에 익지 않아서 JAVA 쓰듯 코딩함. 물론 비주얼 스튜디오 끼고 코딩할때는 줄 잘 맞춤 걱정말아요
List<int> IntegerList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
List<int> IntegerResult = new List<int>();

for(int Count=0 ; Count<IntegerList.Count ; Count++) {
    if(IntegerList[Count] % 2 == 0) {
        IntegerResult.Add(IntegerList[Count]);
    }
}

 

// 아직 C#이 손에 익지 않아서 JAVA 쓰듯 코딩함. 물론 비주얼 스튜디오 끼고 코딩할때는 줄 잘 맞춤 걱정말아요
List<int> IntegerList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
List<int> IntegerResult = new List<int>();

foreach(int IntegerItem in IntegerList) {
    if(IntegerItem % 2 == 0) {
        IntegerResult.Add(IntegerItem);
    }
}

 

// 아직 C#이 손에 익지 않아서 JAVA 쓰듯 코딩함. 물론 비주얼 스튜디오 끼고 코딩할때는 줄 잘 맞춤 걱정말아요
List<int> IntegerList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
List<int> IntegerResult = new List<int>();

IntegerResult = IntegerList.FindAll(Item => Item % 2 == 0);

 

// 아직 C#이 손에 익지 않아서 JAVA 쓰듯 코딩함. 물론 비주얼 스튜디오 끼고 코딩할때는 줄 잘 맞춤 걱정말아요
List<int> IntegerList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
List<int> IntegerResult = new List<int>();

var IntegerFilter = from IntegerItem in IntegerList 
                    where IntegerItem % 2 == 0 
                    select IntegerItem;

foreach(int IntegerItem in IntegerFilter) {
    IntegerResult.Add(IntegerItem);
}

 

// 아직 C#이 손에 익지 않아서 JAVA 쓰듯 코딩함. 물론 비주얼 스튜디오 끼고 코딩할때는 줄 잘 맞춤 걱정말아요
List<int> IntegerList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
List<int> IntegerResult = new List<int>();

var IntegerFilter = from IntegerItem in IntegerList 
                    where IntegerItem % 2 == 0 
                    select IntegerItem;

IntegerResult = IntegerFilter.ToList<int>();

 

너무나도 어이가 없게도, JSON을 파싱해서 유니티 게임기록을 파일에 List 형식으로 관리하고 있는 상황에서 이상하게 기록이 많으면 많아질수록 플레이기록 통계를 내는동안 메인스레드가 머뭇멈칫하는 현상이 있었는데 이걸로 한큐에 해결이 되어버림. Task.Run이고 Coroutine이고 그냥 이거 한발로 해결해버릴 정도로 일단은 만능까지는 검증할 수는 없고 써먹을 구석이 명백히 있는 새로운 문법인듯.

반응형
Comments