{"record":{"id":"2459bcf7a2f01a32","repo":"dotnet/wpf","slug":"expected-a-versionpair-object","errorCode":null,"errorMessage":"Expected a VersionPair object.","messagePattern":"Expected a VersionPair object\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/VersionPair.cs","lineNumber":286,"sourceCode":"\n#if !PBTCOMPILER\n        /// <summary>\n        /// Compare this instance to the object\n        /// </summary>\n        /// <param name=\"obj\">Object to compare</param>\n        /// <returns>Less than 0 - This instance is less than obj\n        /// Zero - This instance is equal to obj\n        /// Greater than 0 - This instance is greater than obj</returns>\n        public int CompareTo(Object obj)\n        {\n            if (obj == null)\n            {\n                return 1;\n            }\n\n            if (obj.GetType() != GetType())\n            {\n                throw new ArgumentException(SR.ExpectedVersionPairObject);\n            }\n\n            VersionPair v = (VersionPair) obj;\n\n            if (this.Equals(obj))   // equal\n            {\n                return 0;\n            }\n            // less than\n            else if (this < v)\n            {\n                return -1;\n            }\n\n            // greater than\n            return 1;\n        }\n#endif","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/VersionPair.cs#L268-L304","documentation":"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.","triggerScenarios":"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.","commonSituations":"Sorting mixed-type collections with ArrayList or non-generic Sort; calling CompareTo(obj) with a System.Version assuming interchangeability.","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"],"exampleFix":"// before\nobject v = Version.Parse(\"1.2\");\nversionPair.CompareTo(v); // throws\n// after\nif (v is VersionPair vp)\n    versionPair.CompareTo(vp);\nelse\n    throw new ArgumentException(\"Expected a VersionPair\");","handlingStrategy":"type-guard","validationCode":"if (obj is not VersionPair) throw new ArgumentException(\"Expected a VersionPair object.\", nameof(obj));","typeGuard":"static bool IsVersionPair(object obj) => obj is VersionPair;","tryCatchPattern":"try { result = versionPair.CompareTo(obj); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"VersionPair\")) { result = -1; /* define ordering for foreign types */ }","preventionTips":["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"],"tags":["type-mismatch","comparison","version"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}