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

  • Home
  • SEARCH
  • 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 270155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:57:14+00:00 2026-05-11T23:57:14+00:00

This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)

  • 0

This code snippet is from C# in Depth

    static bool AreReferencesEqual<T>(T first, T second)
        where T : class
    {
        return first == second;
    }

    static void Main()
    {
        string name = "Jon";
        string intro1 = "My name is " + name;
        string intro2 = "My name is " + name;
        Console.WriteLine(intro1 == intro2);
        Console.WriteLine(AreReferencesEqual(intro1, intro2));
    }

The output of the above code snippet is

True 
False

When the main method is changed to

    static void Main()
    {
        string intro1 = "My name is Jon";
        string intro2 = "My name is Jon";
        Console.WriteLine(intro1 == intro2);
        Console.WriteLine(AreReferencesEqual(intro1, intro2));
    }

The output of the above code snippet is

True 
True

I cannot fathom why ?

EDIT: Once you understand string-interning following questions don’t apply.


How are the parameters received at the Generic method AreReferencesEqual in the second code snippet ?

What changes to the string type when it is concatenated to make the == operator not call the overloaded Equals method of the String type ?

  • 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-11T23:57:14+00:00Added an answer on May 11, 2026 at 11:57 pm

    On the case of strings, you probably don’t intend to use reference equality.
    To access equality and inequality in generic methods, your best bet it:

    EqualityComparer<T>.Default.Equals(x,y); // for equality
    Comparer<T>.Default.Compare(x,y); // for inequality
    

    i.e.

    static bool AreValuesEqual<T>(T first, T second)
        where T : class
    {
        return EqualityComparer<T>.Default.Equals(first,second);
    }
    

    This still uses the overloaded Equals, but handles nulls etc too. For inequality, this handles nulls, and both IComparable<T> and IComparable.

    For other operators, see MiscUtil.


    Re the question; in the case of:

        string intro1 = "My name is Jon";
        string intro2 = "My name is Jon";
        Console.WriteLine(intro1 == intro2);
        Console.WriteLine(AreReferencesEqual(intro1, intro2));
    

    You get true, true because the compiler and runtime is designed to be efficient with strings; any literals that you use are “interned” and the same instance is used every time in your AppDomain. The compiler (rather than runtime) also does the concat if possible – i.e.

        string intro1 = "My name is " + "Jon";
        string intro2 = "My name is " + "Jon";
        Console.WriteLine(intro1 == intro2);
        Console.WriteLine(AreReferencesEqual(intro1, intro2));
    

    is exactly the same code as the previous example. There is no difference at all. However, if you force it to concatenate strings at runtime, it assumes they are likely to be short-lived, so they are not interned/re-used. So in the case:

        string name = "Jon";
        string intro1 = "My name is " + name;
        string intro2 = "My name is " + name;
        Console.WriteLine(intro1 == intro2);
        Console.WriteLine(AreReferencesEqual(intro1, intro2));
    

    you have 4 strings; “Jon” (interned), “My name is ” (interned), and two different instances of “My name is Jon”. Hence == returns true and reference equality returns false. But value-equality (EqualityComparer<T>.Default) would still return true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 166k
  • Answers 166k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This question asks about converting ANSI escape sequences into HTML… May 12, 2026 at 1:19 pm
  • Editorial Team
    Editorial Team added an answer you have to authenticate your webservice by supplying the default… May 12, 2026 at 1:19 pm
  • Editorial Team
    Editorial Team added an answer so your values are 1 and 2, but you want… May 12, 2026 at 1:19 pm

Related Questions

I have the below code, which iterates over a list based on a custom
I'm fairly new to Objective-C and I can't figure out how to wait in
The following code snippet is from book Effective C#, public event AddMessageEventHandler Log; public
I see endless examples that involve Url.Action in a view to generate a URL

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.