Its like IEnumerable FirstOrDefault, but without the Garbage allocation because it operates on a List. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Its like IEnumerable FirstOrDefault, but without the Garbage allocation because it operates on a List.

Demo Code


using System.Linq;
using System;//ww w.  j a v a  2s.  c  om
using System.Collections.Generic;
using System.Collections;
using UnityEngine;

public class Main{
        /// <summary>
        /// Its like IEnumerable FirstOrDefault, but without the Garbage allocation
        /// because it operates on a List. Useful for tight, time sensitive loops. 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <returns></returns>
        public static T FirstOrDefaultQuick<T>(this List<T> self)
        {
            if (self == null)
            {
                return default(T);
            }

            if (self.Count == 0)
            {
                return default(T);
            }

            return self[0];
        }
}

Related Tutorials