No, since this is a violation of the etiquette of a pure function, when a method either has a side effect or returns a useful value (i.e., it doesnβt just indicate an error state), there is never one or the other.
If you want the function to appear as atomic, you can get a lock in the list that will prevent other threads from accessing the list while it is changing, provided that they also use lock :
public static class Extensions { public static T RemoveAndGet<T>(this IList<T> list, int index) { lock(list) { T value = list[index]; list.RemoveAt(index); return value; } } }
Roadierich
source share