{"record":{"id":"3be9a1fa40637bd7","repo":"dotnet/wpf","slug":"sr-cannotcomparedifftypes","errorCode":null,"errorMessage":"SR.CanNotCompareDiffTypes","messagePattern":"SR\\.CanNotCompareDiffTypes","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileStorageReference.cs","lineNumber":85,"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</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            // Note that because of the GetType() checking above, the casting must be valid.\n            CompoundFileStorageReference r = (CompoundFileStorageReference)o;\n            return String.CompareOrdinal(_fullName.ToUpperInvariant(), r._fullName.ToUpperInvariant());\n        }\n        #endregion\n\n        //------------------------------------------------------\n        //\n        //   Private Methods\n        //\n        //------------------------------------------------------\n        /// <summary>\n        /// Assign the fullName\n        /// </summary>\n        /// <param name=\"fullName\">name</param>\n        /// <remarks>cache a duplicate copy of the storage name to save having to do this for ","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileStorageReference.cs#L67-L103","documentation":"CompoundFileStorageReference.CompareTo(object) requires its argument to be another CompoundFileStorageReference. When passed a non-null object of a different type it throws ArgumentException with SR.CanNotCompareDiffTypes. Comparisons are done ordinal-insensitively on the upper-invariant full name, which only makes sense between same-type references.","triggerScenarios":"Calling CompareTo on a CompoundFileStorageReference and passing an object of any other type (e.g. a CompoundFileStreamReference, string, or unrelated class); also occurs indirectly via Sort()/ArrayList.Sort on mixed-type collections.","commonSituations":"Sorting mixed lists of storage and stream references in custom packaging tools, comparing references from different compound-file implementations, or passing parsed strings instead of reference objects.","solutions":["Ensure every element compared is a CompoundFileStorageReference; filter or partition mixed collections before sorting.","Use type-safe generic collections (List<CompoundFileStorageReference>) so the compiler enforces element type.","Compare against null first if you want the standard 'any reference sorts after null' behavior (CompareTo already returns 1 for null).","If comparing across reference kinds, compare on a shared key (e.g. FullName string) instead of CompareTo."],"exampleFix":"// before\nlist.Contains(mixedObject); // mixed List<object> sorted -> ArgumentException\n// after\nvar storages = list.OfType<CompoundFileStorageReference>().ToList();\nstorages.Sort(); // only same-type references compared","handlingStrategy":"type-guard","validationCode":"if (other is CompoundFileStorageReference) { /* safe to sort */ }","typeGuard":"bool IsComparableStorageRef(object o) => o is CompoundFileStorageReference;","tryCatchPattern":"try { result = storageRef.CompareTo(obj); }\ncatch (ArgumentException) { result = string.CompareOrdinal(storageRef.FullName, obj?.ToString()); }","preventionTips":["Use strongly typed collections instead of ArrayList/object[]","Filter with OfType<T>() before sorting","Never mix storage and stream references in one sorted list"],"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"}