Repository: dotnet/runtime
File: src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs (C#)

Code before:
                        }
                        else
                        {
                            throw new InvalidOperationException(SR.Format(SR.AsyncDisposableServiceDispose, TypeNameHelper.GetTypeDisplayName(toDispose[index])));
                        }
                    }
                }
                catch (Exception ex)
                {
                    exceptions ??= new List<Exception>();
                    exceptions.Add(ex);
                    index--;
                }
            }

            if (exceptions is null)
            {
                return;
            }

            if (exceptions.Count == 1)
            {
                throw exceptions[0];
            }

            throw new AggregateException(exceptions);
        }

        public ValueTask DisposeAsync()
        {
            List<object>? toDispose = BeginDispose();
            var exceptions = default(List<Exception>);
            var index = toDispose is null ? -1 : toDispose.Count - 1;

            while (index >= 0)
            {
                try
                {
                    for (; index >= 0; index--)
                    {
                        object disposable = toDispose![index];
                        if (disposable is IAsyncDisposable asyncDisposable)
                        {
                            ValueTask vt = asyncDisposable.DisposeAsync();
                            if (!vt.IsCompletedSuccessfully)
                            {
                                return Await(index, vt, toDispose);
                            }

                            // If its a IValueTaskSource backed ValueTask,
                            // inform it its result has been read so it can reset

Review comment (suggestion):
More succinct?
```suggestion
            throw exceptions is [var ex] ? ex : new AggregateException(exceptions);
```

Code after:
                    {
                        disposable.Dispose();
                    }
                    else
                    {
                        throw new InvalidOperationException(SR.Format(SR.AsyncDisposableServiceDispose, TypeNameHelper.GetTypeDisplayName(toDispose[i])));
                    }
                }
                catch (Exception exception)
                {
                    AddExceptionToCache(ref exceptionsCache, exception);
                }
            }

            CheckExceptionCache(exceptionsCache);
        }

        public ValueTask DisposeAsync()
        {
            List<object>? toDispose = BeginDispose();
            if (toDispose is null)
            {
                return default;
            }

            object? exceptionsCache = null;
            for (var i = toDispose.Count - 1; i >= 0; i--)
            {
                try
                {
                    object disposable = toDispose[i];
                    if (disposable is IAsyncDisposable asyncDisposable)
                    {
                        ValueTask vt = asyncDisposable.DisposeAsync();
                        if (!vt.IsCompletedSuccessfully)
                        {
                            return Await(i, vt, toDispose, exceptionsCache);
                        }

                        // If its a IValueTaskSource backed ValueTask,
                        // inform it its result has been read so it can reset
                        vt.GetAwaiter().GetResult();
                    }
                    else
                    {
                        ((IDisposable)disposable).Dispose();
                    }
                }
                catch (Exception exception)
                {
                    AddExceptionToCache(ref exceptionsCache, exception);