Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:36:13+00:00 2026-05-31T12:36:13+00:00

string s1 = t; string s2 = ‘t’.ToString(); Console.WriteLine(s1.Equals(s2)); // returning true Console.WriteLine(object.Equals(s1, s2));

  • 0
        string s1 = "t";
        string s2 = 't'.ToString();        

        Console.WriteLine(s1.Equals(s2)); // returning true
        Console.WriteLine(object.Equals(s1, s2)); // returning true

Here it is returning same result. Now when I’m using StringBuilder it is not returning same value. What is the underneath reason?

        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();

        Console.WriteLine(s1.Equals(s2)); // returning true
        Console.WriteLine(object.Equals(s1, s2)); // returning false

Edit1: My above question answered below. But during this discussion what we find out StringBuilder doesn’t have any override Equals method in its implementation. So when we call StringBuilder.Equals it actually goes to Object.Equals. So if someone calls StringBuilder.Equals and S1.Equals(S2) the result will be different.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:36:14+00:00Added an answer on May 31, 2026 at 12:36 pm

    String.Equals() is overriden in C# such that identical strings are in fact Equal() when the Equal() override defined on string is used.

    If you are comparing string literals (not the case in your example), it’s worth noting that identical string literals are interned… that is, identical strings live at the same address so will also be equal by reference (e.g. object.Equals() or s1.ReferenceEquals(s2)) as well as by value.

    StringBuilder provides an overload to Equals() that takes StringBuilder as a parameter (that is s1.Equals(s2) will call that overload instead of calling object.Equals(object obj)).

    http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.equals.aspx

    StringBuilder.Equals() is…

    true if this instance and sb have equal string, Capacity, and
    MaxCapacity values; otherwise, false.

    object.Equals() uses the static Equals() defined on object, which checks only for reference equality (if passed a class) or for value equality (if passed a struct).

    So in summary

    string s1 = "t";
    string s2 = 't'.ToString();        
    
    Console.WriteLine(s1.Equals(s2)); // true because both reference equality (interned strings) and value equality (string overrides Equals())
    Console.WriteLine(object.Equals(s1, s2)); // true because of reference equality (interned strings)
    
    StringBuilder s1 = new StringBuilder();
    StringBuilder s2 = new StringBuilder();
    
    Console.WriteLine(s1.Equals(s2)); // true because StringBuilder.Equals() overloaded
    Console.WriteLine(object.Equals(s1, s2)); // false because the two StringBuilder instances have different addresses (references not equal)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

String object behaves like a Value type when using == and != operators which
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
string Newfilename; string Defaultfilename; protected void btnup_Click(object sender, EventArgs e) { if (ASPxUploadControl1.HasFile) {
string[] usersToAdd = new string[] { asd, asdert, gasdff6 }; using (Entities context =
string s; getline(cin,s); while (HOW TO WRITE IT HERE?) { inputs.push_back(s); getline(cin,s); }
String query = CREATE TABLE \ EtherMap\.\ table_name\( feature_name (20)); Here feature_name is the
string ouString = projectBox.Text.ToString(); string parentName = LDAP://OU=+ouString+,OU=Clients,OU=Clients,DC=domain,DC=net; DirectoryEntry parentEntry = new DirectoryEntry(parentName); DirectoryEntry
string emailfield=txtEmail.Text.ToString(); string url = http://localhost:3076/user/Authenticate-Users.aspx?email=+emailfield; I want to encrypt the querystring and then
string = heLLo hOw are you toDay results = string.find([A-Z]) <----- Here is my
String s; /*code*/ s = foo; Is a whole new object being created, since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.