Compare String and Object in C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, comparing a string with an object is not one single operation. First you need to know whether the object actually contains a string, and then you need to decide whether you care about textual equality, reference identity, or a formatted string representation. If you skip those distinctions, the code often works accidentally on one input and fails on the next.
Start with the Type Relationship
Every string is an object, because string derives from System.Object. But an object variable can hold almost anything at runtime.
That means the first step is often a runtime type check, not the comparison itself.
Value Equality Is Usually What You Want
When comparing text, you usually care about character equality, not whether both variables refer to the exact same object instance.
Typical output is:
That is why ReferenceEquals is normally the wrong tool for string comparison.
Compare Safely When the Other Value Is Typed as object
Suppose you have a string and an object whose runtime type is uncertain. Pattern matching is the safest comparison style.
This approach does two useful things:
- it checks that the object really contains a string,
- and it applies an explicit comparison rule.
That is much better than a blind cast.
Avoid ToString Unless That Is Truly the Comparison You Mean
A common shortcut is to convert the object to text first.
This may produce true, but it changes the semantics. You are no longer asking whether the object is a string. You are asking whether the object’s string representation happens to render the same text.
Those are different questions. A DateTime, a number, or a custom type may format differently depending on culture or implementation details.
So use ToString only when comparing rendered output is exactly the real requirement.
object.Equals Versus string.Equals
object.Equals(x, y) is useful when you want a null-safe general equality check. string.Equals is better when your intent is specifically string comparison with a defined comparison mode.
If the values are known or expected to be strings, string.Equals communicates that intent more clearly.
Pick the Right StringComparison
This is where many subtle bugs come from. Ordinal compares raw character values. Culture-aware options are appropriate for user-facing comparisons. Case-insensitive comparisons should also be explicit.
Do not rely on default comparison behavior when correctness depends on case or culture.
Common Pitfalls
- Comparing a string to an
objectwithout first checking whether the object actually contains a string. - Using
ReferenceEqualswhen the real goal is text equality. - Calling
ToStringon arbitrary objects and assuming that means the object “is” the same string. - Omitting the
StringComparisonargument when case or culture rules matter. - Blind-casting the object to
stringand risking an invalid cast or a null issue.
Summary
- A
stringis anobject, but anobjectis not necessarily a string. - For text comparison, prefer
string.Equalsor==over reference identity checks. - Pattern matching is a safe way to compare a string to an
objectwith uncertain runtime type. - Use
ToStringonly when formatted text is the real comparison target. - Always choose an explicit
StringComparisonwhen comparison rules matter.

