BreakingAndStoppingParallelFor.Program.GetValuesBeforeValue C# (CSharp) Method

GetValuesBeforeValue() public static method

public static GetValuesBeforeValue ( List values, int stopperValue ) : List
values List
stopperValue int
return List
        public static List<int> GetValuesBeforeValue(List<int> values, int stopperValue)
        {
            ConcurrentStack<int> valuesToReturn = new ConcurrentStack<int>();

            Parallel.For(0, values.Count, (index, loopState) =>
            {
                if (values[index] != stopperValue)
                {
                    valuesToReturn.Push(values[index]);
                }
                else
                {
                    loopState.Break();
                }
            });

            return valuesToReturn.ToList();
        }