dotnet/wpf · error · ArgumentException
Expected a VersionPair object.
Error message
Expected a VersionPair object.
What it means
VersionPair.CompareTo(object) throws ArgumentException when the passed object is not null/other VersionPair and its runtime type differs from VersionPair's. IComparable semantics require comparing only compatible types, so a foreign type is rejected rather than mis-compared.
Solutions
- Only pass VersionPair instances to CompareTo; convert System.Version to VersionPair first
- Use generic collections (Sort<VersionPair>) to get compile-time type safety
- Check obj is VersionPair before calling CompareTo
Example fix
// before
object v = Version.Parse("1.2");
versionPair.CompareTo(v); // throws
// after
if (v is VersionPair vp)
versionPair.CompareTo(vp);
else
throw new ArgumentException("Expected a VersionPair"); Defensive patterns
Strategy: type-guard
Validate before calling
if (obj is not VersionPair) throw new ArgumentException("Expected a VersionPair object.", nameof(obj)); Type guard
static bool IsVersionPair(object obj) => obj is VersionPair;
Try / catch
try { result = versionPair.CompareTo(obj); }
catch (ArgumentException ex) when (ex.Message.Contains("VersionPair")) { result = -1; /* define ordering for foreign types */ } Prevention
- Use generic sorted collections (List<VersionPair>.Sort) so only VersionPair is compared
- Convert System.Version to VersionPair before comparisons
- Never call non-generic IComparable.CompareTo with mixed-type collections
When it happens
Trigger: Passing a non-VersionPair object (e.g. System.Version, string, int) to CompareTo or to sorted collections/calls that invoke it, such as List<VersionPair>.BinarySearch with a boxed non-VersionPair value.
Common situations: Sorting mixed-type collections with ArrayList or non-generic Sort; calling CompareTo(obj) with a System.Version assuming interchangeability.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot compare different types.
- SR.CanNotCompareDiffTypes
- ' ' is not a Visual or Visual3D.
- 0x80070057
- Animation_AnimationTimelineTypeMismatch
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2459bcf7a2f01a32.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/VersionPair.cs:286
#if !PBTCOMPILER
/// <summary>
/// Compare this instance to the object
/// </summary>
/// <param name="obj">Object to compare</param>
/// <returns>Less than 0 - This instance is less than obj
/// Zero - This instance is equal to obj
/// Greater than 0 - This instance is greater than obj</returns>
public int CompareTo(Object obj)
{
if (obj == null)
{
return 1;
}
if (obj.GetType() != GetType())
{
throw new ArgumentException(SR.ExpectedVersionPairObject);
}
VersionPair v = (VersionPair) obj;
if (this.Equals(obj)) // equal
{
return 0;
}
// less than
else if (this < v)
{
return -1;
}
// greater than
return 1;
}
#endifView on GitHub (pinned to 81131a70a4)