Repository: dotnet/runtime
File: src/libraries/System.Linq/src/System/Linq/Select.SizeOpt.cs (C#)

Code before:
            public override bool MoveNext()
            {
                switch (_state)
                {
                    case 1:
                        _enumerator = _source.GetEnumerator();
                        _state = 2;
                        goto case 2;
                    case 2:
                        Debug.Assert(_enumerator is not null);
                        if (_enumerator.MoveNext())
                        {
                            _current = _selector(_enumerator.Current);
                            return true;
                        }

                        Dispose();
                        break;
                }

                return false;
            }

            public override void Dispose()
            {
                if (_enumerator is not null)
                {
                    _enumerator.Dispose();
                    _enumerator = null;
                }

                base.Dispose();
            }

            public override TResult[] ToArray()
            {
                TResult[] array = new TResult[_source.Count];
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = _selector(_source[i]);
                }
                return array;
            }

            public override List<TResult> ToList()
            {
                List<TResult> list = new List<TResult>(_source.Count);
                for (int i = 0; i < list.Count; i++)
                {
                    list.Add(_selector(_source[i]));
                }

Review comment (suggestion):
@copilot, please restructure these as:
```c#
if (_enumerator is {} e)
{
    _enumerator = null;
    e.Dispose()
}

base.Dispose();
```

Code after:
            public override bool MoveNext()
            {
                switch (_state)
                {
                    case 1:
                        _enumerator = _source.GetEnumerator();
                        _state = 2;
                        goto case 2;
                    case 2:
                        Debug.Assert(_enumerator is not null);
                        if (_enumerator.MoveNext())
                        {
                            _current = _selector(_enumerator.Current);
                            return true;
                        }

                        Dispose();
                        break;
                }

                return false;
            }

            public override void Dispose()
            {
                if (_enumerator is { } e)
                {
                    _enumerator = null;
                    e.Dispose();
                }

                base.Dispose();
            }

            public override TResult[] ToArray()
            {
                TResult[] array = new TResult[_source.Count];
                for (int i = 0; i < array.Length; i++)
                {
                    array[i] = _selector(_source[i]);
                }
                return array;
            }

            public override List<TResult> ToList()
            {
                List<TResult> list = new List<TResult>(_source.Count);
                for (int i = 0; i < list.Count; i++)
                {
                    list.Add(_selector(_source[i]));
                }