본문 바로가기

C#

[C#] LINQ - Any()

출처 : https://learn.microsoft.com/ko-kr/dotnet/api/system.linq.enumerable.any?view=net-8.0

 

IEnumerable.Any()

정의

  • namespace  :  System.Linq
  • 시퀀스에 요소가 하나라도 있는지 또는 특정 조건에 맞는 요소가 있는지 확인 후 bool 값을 반환합니다.

 

오버로드

  • IEnumerable.Any(System.Func(TSource, Boolean))

 

반환

  • Boolean
  • 시퀀스에 요소가 하나라도 있으면 true, 아니라면 false

 

예시

  • 오버로드된 Any 메서드 사용
  • 풀링 시 Contains 메서드나 " == " 및 " Equal "을 통해 객체를 중복으로 사용할 수 있기 때문에
    Linq.Any() 와 ReferenceEquals() 메서드로 참조 값 검사를 하는 상황
public List<CGameObject> m_lstmyObjects_Use = new List<CGameObject>();

                               // ReferenceEquals 메서드는 객체의 메모리 주소(참조)를 비교하여 동일 객체인지 확인
if (m_lstmyObjects_Use.Any(obj => ReferenceEquals(obj, mo_Object)) == false)
    m_lstmyObjects_Use.Add(mo_Object);