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 7573921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:18:14+00:00 2026-05-30T16:18:14+00:00

What is the difference between == and Equals() with example? I know that ==

  • 0

What is the difference between == and Equals() with example? I know that == is used to compare operator and Equals() method is used to compare content of string.So i tried

// first example
string s1 = "a";
string s2 = "a";
Console.Write(a.Equals(s2)); // returns true, but if I assign "b" to s2,
                             // then result will be false

// second example
string s1 ="a";
string s2 ="a";
Console.Write(s1 == s2);     // returns true

How this is so? Both are different object references. Suppose we consider that these are reference. But I tried to use like this

string s1 = new string("ab");
string s2 = new string("ab");

I am getting compile time error that can not convert string to char

  • 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-30T16:18:15+00:00Added an answer on May 30, 2026 at 4:18 pm

    There are several things going on. Firstly, in this example:

    string s1 = "a";
    string s2 = "a";
    Console.WriteLine(s1 == s2);
    

    You claim that:

    Both are different object reference.

    That’s not true due to string interning. s1 and s2 are references to the same object. The C# specification guarantees that – from section 2.4.4.5 of the C# 4 specification:

    When two or more string literals that are equivalent according to the string equality operator (§7.10.7) appear in the same program, these string literals refer to the same string instance.

    So in this particular case, you would still get “true” even if you printed object.ReferenceEquals(s1, s2), or if you made it use a true reference identity comparison with ==:

    object s1 = "a";
    object s2 = "a";
    Console.WriteLine(s1 == s2); // Still prints True due to string literal interning
    

    However, even if these were references to separate objects, == is overloaded for string. Overloading is a compile-time decision – the implementation to use depends on the compile-time types of the operands. So for example:

    string a = new string('x', 1);
    string b = new string('x', 1);
    Console.WriteLine(a == b); // Uses string's implementation, prints True
    
    object c = a;
    object d = b;
    Console.WriteLine(c == d); // Reference identity comparison, prints False
    

    Compare that with object.Equals(object) which is a virtual method. As it happens, String overloads this method as well, but importantly it overrides it. So if we change our code to:

    string a = new string('x', 1);
    string b = new string('x', 1);
    Console.WriteLine(a.Equals((object) b));
    
    object c = a;
    object d = b;
    Console.WriteLine(c.Equals(d));
    

    … then both method calls in the compiled code will simply be to object.Equals(object), but they’ll still both print True because of polymorphism: the implementation in String will be used.

    Here’s what a call to the overloaded method would look like:

    string a = new string('x', 1);
    string b = new string('x', 1);
    Console.WriteLine(a.Equals(b)); // Calls string.Equals(string)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The difference between Chr and Char when used in converting types is that one
Possible Duplicates: Java String.equals versus == whats the difference between ".equals and ==" public
Autoboxing is rather scary. While I fully understand the difference between == and .equals
Difference between a bus error and a segmentation fault? Can it happen that a
In JavaScript, is there a performance difference between using a double equals ( ==
Is there any difference between & and + operators while concatenating string? if yes,
What is the difference between == and .equals() in Scala, and when to use
What's the exact difference between the two? // When calling this method with GetByType<MyClass>()
What is the difference between the evaluation of == and Equals in C#? For
What is the difference between early and late binding?

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.