stride3d/stride · error · ArgumentException

The type of this collection does not implement any…

Error message

The type of this collection does not implement any IComparable interface and no IComparer has been provided

What it means

SortedObservableCollection must know how to order its items. At construction, it builds DefaultCompareFunc from a supplied IComparer, a comparer type, or T's IComparable implementation. If none is available it throws ArgumentException, since a sorted collection without comparison cannot insert items.

Solutions

  1. Pass an IComparer<T> (or comparison lambda, per constructor overloads) to the constructor
  2. Implement IComparable<T> on the item type T
  3. Use a constructor overload accepting a Comparison<T> delegate
  4. Verify T didn't change to a non-comparable type after a refactor

Example fix

// before
var col = new SortedObservableCollection<MyVm>();
// after
var col = new SortedObservableCollection<MyVm>(Comparer<MyVm>.Create((a, b) => a.Name.CompareTo(b.Name)));
Defensive patterns

Strategy: validation

Validate before calling

if (typeof(IComparable<MyVm>).IsAssignableFrom(typeof(MyVm)) || comparer != null) { /* safe to construct */ }

Try / catch

try { var col = new SortedObservableCollection<MyVm>(); } catch (ArgumentException) { var col = new SortedObservableCollection<MyVm>(defaultComparer); }

Prevention

When it happens

Trigger: Constructing SortedObservableCollection<T> where T does not implement IComparable and no comparer is passed; passing a null comparer; specifying a comparer type that cannot be instantiated.

Common situations: Using plain POCO/view-model classes without IComparable as items; refactoring removed an IComparable implementation; forgetting the comparer constructor argument after changing T.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/8c3252b63f74bae4. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs:84

            {
                DefaultCompareFunc = (item1, item2) => ((IComparable)item1!).CompareTo(item2);
            }
            else
            {
                DefaultCompareFunc = (item1, item2) =>
                {
                    if (item1 is null && item2 is null)
                        return 0;

                    return item1 is not null ? ((IComparable)item1).CompareTo(item2) : -((IComparable)item2!).CompareTo(item1);
                };
            }
        }

        // If we have no comparer at this point we're dead
        if (DefaultCompareFunc is null)
        {
            throw new ArgumentException("The type of this collection does not implement any IComparable interface and no IComparer has been provided");
        }
    }

    /// <summary>
    /// Seach the index of an item.
    /// </summary>
    /// <param name="item">The item to search.</param>
    /// <returns>The index of the item in the collection, or -1 if the item does not exist in the collection.</returns>
    public int BinarySearch(T item)
    {
        return GetIndex(item, false);
    }

    /// <summary>
    /// Searches the index of key in the collection, using a provided function to compare items to the key.
    /// </summary>
    /// <typeparam name="TSearch">The type of the key to search.</typeparam>
    /// <param name="key">The key to search in the collection.</param>

View on GitHub (pinned to 96fad776d2)