{"record":{"id":"df4fbba77ddc5e0f","repo":"git-ecosystem/git-credential-manager","slug":"a-gitversion-object-is-required-for-comparison","errorCode":null,"errorMessage":"A GitVersion object is required for comparison.","messagePattern":"A GitVersion object is required for comparison\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Core/GitVersion.cs","lineNumber":72,"sourceCode":"                {\n                    return ToString();\n                }\n\n                return _originalString;\n            }\n        }\n\n        public int CompareTo(object obj)\n        {\n            if (obj is null)\n            {\n                return 1;\n            }\n\n            GitVersion other = obj as GitVersion;\n            if (other == null)\n            {\n                throw new ArgumentException(\"A GitVersion object is required for comparison.\", \"obj\");\n            }\n\n            return CompareTo(other);\n        }\n\n        public int CompareTo(GitVersion other)\n        {\n            if (other is null)\n            {\n                return 1;\n            }\n\n            // Compare for as many components as the two versions have in common. If a\n            // component does not exist in a components list, it is assumed to be 0.\n            int thisCount = _components.Count, otherCount = other._components.Count;\n            for (int i = 0; i < Math.Max(thisCount, otherCount); i++)\n            {\n                int thisComponent = i < thisCount ? _components[i] : 0;","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/GitVersion.cs#L54-L90","documentation":"GitVersion.CompareTo(object) accepts only GitVersion instances; comparing against any other type (including strings like \"2.40.0\" or ints) throws ArgumentException, per the IComparable contract. Use GitVersion.Parse for strings or the strongly-typed CompareTo(GitVersion) overload.","triggerScenarios":"Calling object-typed CompareTo (e.g. via IComparable in Array.Sort, List.Sort with non-generic comparisons, or Comparer<object>) with a boxed non-GitVersion value such as a string.","commonSituations":"Sorting a mixed collection where one element is a string version; comparing GitVersion directly to a string literal with .CompareTo(\"2.33.0\"); reflection-based comparisons.","solutions":["Compare GitVersion to GitVersion: GitVersion.Parse(\"2.33.0\").CompareTo(version).","Use typed collections (List<GitVersion>) so the generic comparer is used.","In custom sort logic, call GitVersion.Parse on strings before comparison."],"exampleFix":"// before\nversion.CompareTo(\"2.33.0\") // ArgumentException\n// after\nversion.CompareTo(GitVersion.Parse(\"2.33.0\"))","handlingStrategy":"type-guard","validationCode":"// before comparing:\nif (obj is not GitVersion other) throw new ArgumentException(nameof(obj));\nint cmp = other.CompareTo(version);","typeGuard":"bool isGitVersion(object o) => o is GitVersion;","tryCatchPattern":"try { result = version.CompareTo(obj); }\ncatch (ArgumentException) { result = version.CompareTo(GitVersion.Parse(obj.ToString())); }","preventionTips":["Always compare with the typed CompareTo(GitVersion) overload.","Parse strings with GitVersion.Parse before any comparison.","Keep collections typed as GitVersion so generic sorters use the right comparer."],"tags":["csharp","type-mismatch","icomparable","versioning"],"backgroundTag":"type-mismatch","analyzedSha":"e8ce762cd04b4100ae637b5fbf39ef9d0a96561e","analyzedAt":"2026-09-11T17:15:08.753Z","contentChangedAt":"2026-09-11T17:15:08.753Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}