Repository: dotnet/maui
File: src/Controls/src/Core/Picker/Picker.cs (C#)

Code before:
				// Remove e.OldItems.Count items starting at e.OldStartingIndex
				removeStart = e.OldStartingIndex;
				index = e.OldStartingIndex + e.OldItems.Count - 1;
			}
			else
			{
				// Remove e.OldItems.Count items at the end when e.OldStartingIndex is past the end of the Items collection
				removeStart = Items.Count - e.OldItems.Count;
				index = Items.Count - 1;
			}

			foreach (object _ in e.OldItems)
				((LockableObservableListWrapper)Items).InternalRemoveAt(index--);

			index = GetSelectedIndex();
			if (removeStart <= index)
			{
				ClampSelectedIndex(index);
			}
		}

		int GetSelectedIndex()
		{
			if (SelectedItem is not null && (ItemsSource is not null || Items is not null))
			{
				int newIndex = ItemsSource?.IndexOf(SelectedItem) ?? Items.IndexOf(SelectedItem);
				if (newIndex >= 0)
				{
					return newIndex;
				}
			}

			return SelectedIndex;
		}

		void ResetItems()
		{
			if (ItemsSource == null)
				return;
			((LockableObservableListWrapper)Items).InternalClear();
			foreach (object item in ItemsSource)
				((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item));
			Handler?.UpdateValue(nameof(IPicker.Items));

			ClampSelectedIndex(SelectedIndex);
		}

		static void OnSelectedIndexChanged(object bindable, object oldValue, object newValue)
		{
			var picker = (Picker)bindable;
			picker.UpdateSelectedItem(picker.SelectedIndex);

Review comment (suggestion):
Already are checking if ItemsSource is not null before, redundant check here. Can remove the ?, or modify the code:

```
if (SelectedItem is null)
{
    return SelectedIndex;
}

int newIndex = ItemsSource?.IndexOf(SelectedItem) ?? Items?.IndexOf(SelectedItem) ?? -1;

return newIndex >= 0 ? newIndex : SelectedIndex;
```

Code after:
			while (_pendingIsOpenActions.Count > 0 && Handler != null)
			{
				var action = _pendingIsOpenActions.Dequeue();
				action.Invoke();
			}
		}

		void HandleIsOpenChanged()
		{
			if (Handler?.VirtualView is not Picker picker)
				return;

			if (picker.IsOpen)
				picker.Opened?.Invoke(picker, PickerOpenedEventArgs.Empty);
			else
				picker.Closed?.Invoke(picker, PickerClosedEventArgs.Empty);
		}

		void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			switch (e.Action)
			{
				case NotifyCollectionChangedAction.Add:
					AddItems(e);
					break;
				case NotifyCollectionChangedAction.Remove:
					RemoveItems(e);
					break;
				default: //Move, Replace, Reset
					ResetItems();
					break;
			}

			Handler?.UpdateValue(nameof(IPicker.Items));
		}

		void AddItems(NotifyCollectionChangedEventArgs e)
		{
			int insertIndex = e.NewStartingIndex < 0 ? Items.Count : e.NewStartingIndex;
			int index = insertIndex;
			foreach (object newItem in e.NewItems)
				((LockableObservableListWrapper)Items).InternalInsert(index++, GetDisplayMember(newItem));

			index = GetSelectedIndex();
			if (insertIndex <= index)
			{
				// When an item is inserted before the current selection, the selected item changes because the selected index is not properly updated.
				ClampSelectedIndex(index);
			}
		}