Extension method for NativeList ContainsAll help

Hi there,

Im trying to write an extension method that checks if a nativelist contains same value. Not sure if following is right approach, please advise.

        public static bool ContainsAll<T>(this NativeList<T> collection, in T value)
            where T : unmanaged
        {
            var comparer = EqualityComparer<T>.Default;

            for (int i = collection.Length - 1; i >= 0; i--)
            {
                if (!comparer.Equals(collection[i], value))
                {
                    return false;
                }
            }
       
            return true;
        }

Does it do what you want it to do?
Does it compile in Burst?
Does it perform as fast as you’d like?

If you answered “yes” to all three of these, then it is the right approach.

Note: This isn’t a general rule, but specific to what you’ve shared.

1 Like