C#
String
Object
Programming
C# Comparison

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.

csharp
1using System;
2
3object value = "hello";
4string text = "hello";
5
6Console.WriteLine(value is string);
7Console.WriteLine(text is object);

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.

csharp
1using System;
2
3string a = "hello";
4string b = new string(new[] { 'h', 'e', 'l', 'l', 'o' });
5
6Console.WriteLine(a == b);
7Console.WriteLine(string.Equals(a, b, StringComparison.Ordinal));
8Console.WriteLine(object.ReferenceEquals(a, b));

Typical output is:

text
True
True
False

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.

csharp
1using System;
2
3string expected = "hello";
4object candidate = GetValue();
5
6bool same = candidate is string text &&
7            string.Equals(expected, text, StringComparison.Ordinal);
8
9Console.WriteLine(same);
10
11static object GetValue() => "hello";

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.

csharp
object value = 123;
bool match = "123" == value.ToString();

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.

csharp
1using System;
2
3object x = "hello";
4object y = "hello";
5
6Console.WriteLine(object.Equals(x, y));
7Console.WriteLine(string.Equals(x as string, y as string, StringComparison.Ordinal));

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.

csharp
1using System;
2
3string expected = "Hello";
4object candidate = "hello";
5
6bool same = candidate is string text &&
7            string.Equals(expected, text, StringComparison.OrdinalIgnoreCase);
8
9Console.WriteLine(same);

Do not rely on default comparison behavior when correctness depends on case or culture.

Common Pitfalls

  • Comparing a string to an object without first checking whether the object actually contains a string.
  • Using ReferenceEquals when the real goal is text equality.
  • Calling ToString on arbitrary objects and assuming that means the object “is” the same string.
  • Omitting the StringComparison argument when case or culture rules matter.
  • Blind-casting the object to string and risking an invalid cast or a null issue.

Summary

  • A string is an object, but an object is not necessarily a string.
  • For text comparison, prefer string.Equals or == over reference identity checks.
  • Pattern matching is a safe way to compare a string to an object with uncertain runtime type.
  • Use ToString only when formatted text is the real comparison target.
  • Always choose an explicit StringComparison when comparison rules matter.

Course illustration
Course illustration

All Rights Reserved.