{"record":{"id":"57ade77f6b95fe71","repo":"dotnet/wpf","slug":"cannot-compare-different-types","errorCode":null,"errorMessage":"Cannot compare different types.","messagePattern":"Cannot compare different types\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileStreamReference.cs","lineNumber":106,"sourceCode":"\n        #endregion\n\n        #region IComparable\n        /// <summary>\n        /// Compares two CompoundFileReferences\n        /// </summary>\n        /// <param name=\"o\">CompoundFileReference to compare to this one</param>\n        /// <remarks>Supports the IComparable interface</remarks>\n        /// <returns>Less than zero if this instance is less than the given reference, zero if they are equal\n        /// and greater than zero if this instance is greater than the given reference.  Not case sensitive.</returns>\n        int IComparable.CompareTo(object o)\n        {\n            if (o == null)\n                return 1;   // Standard behavior.\n\n            // different type?\n            if (o.GetType() != GetType())\n                throw new ArgumentException(\n                    SR.CanNotCompareDiffTypes);\n\n            CompoundFileStreamReference r = (CompoundFileStreamReference)o;\n            return String.CompareOrdinal(_fullName.ToUpperInvariant(), r._fullName.ToUpperInvariant());\n        }\n        #endregion\n\n        //------------------------------------------------------\n        //\n        //   Private Methods\n        //\n        //------------------------------------------------------\n        /// <summary>\n        /// Initialize _fullName\n        /// </summary>\n        /// <remarks>this should only be called from constructors as references are immutable</remarks>\n        /// <param name=\"fullName\">string to parse</param>\n        /// <exception cref=\"ArgumentException\">if leading or trailing path delimiter</exception>","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileStreamReference.cs#L88-L124","documentation":"CompoundFileStreamReference.CompareTo(object) only supports comparing against another CompoundFileStreamReference. Passing any other non-null object type throws ArgumentException (SR.CanNotCompareDiffTypes). The comparison itself is an ordinal compare of the upper-invariant full names.","triggerScenarios":"Calling CompareTo on a CompoundFileStreamReference with a non-null argument of a different type — e.g. a CompoundFileStorageReference, a string, or an element of an object[]/ArrayList during Sort.","commonSituations":"Sorting heterogeneous reference collections in packaging utilities, mixing stream and storage references in one list, or accidentally boxing/typing arguments as object.","solutions":["Keep stream references in a strongly typed List<CompoundFileStreamReference> before sorting.","Separate storages and streams into different collections and sort each independently.","Filter with OfType<CompoundFileStreamReference>() before any Sort/CompareTo.","If a cross-type ordering is needed, implement a custom comparer over a common property like FullName."],"exampleFix":"// before\nArrayList refs = GetMixedReferences();\nrefs.Sort(); // ArgumentException on mixed types\n// after\nvar streamRefs = GetMixedReferences().OfType<CompoundFileStreamReference>().ToList();\nstreamRefs.Sort();","handlingStrategy":"type-guard","validationCode":"if (other is CompoundFileStreamReference) { /* safe to compare */ }","typeGuard":"bool IsComparableStreamRef(object o) => o is CompoundFileStreamReference;","tryCatchPattern":"try { result = streamRef.CompareTo(obj); }\ncatch (ArgumentException) { result = -1; /* or custom cross-type ordering */ }","preventionTips":["Keep stream references in List<CompoundFileStreamReference>","Sort storages and streams in separate collections","Use a custom IComparer over FullName for mixed-type ordering"],"tags":["argument","comparison","type-mismatch","packaging"],"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"}