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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:07:15+00:00 2026-05-14T07:07:15+00:00

I am relatively new to C#, and I noticed something interesting today that I

  • 0

I am relatively new to C#, and I noticed something interesting today that I guess I have never noticed or perhaps I am missing something. Here is an NUnit test to give an example:

object boolean1 = false;
object booloan2 = false;
Assert.That(boolean1 == booloan2);

This unit test fails, but this one passes:

object string1 = "string";
object string2 = "string";
Assert.That(string1 == string2);

I’m not that surprised in and of itself that the first one fails seeing as boolean1, and boolean2 are different references. But it is troubling to me that the first one fails, and the second one passes. I read (on MSDN somewhere) that some magic was done to the String class to facilitate this. I think my question really is why wasn’t this behavior replicated in bool? As a note… if the boolean1 and 2 are declared as bool then there is no problem.

What is the reason for these differences or why it was implemented that way? Is there a situation where you would want to reference a bool object for anything except its value?

  • 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-14T07:07:16+00:00Added an answer on May 14, 2026 at 7:07 am

    It’s because the strings are in fact referring the same instance. Strings are interned, so that unique strings are reused. This means that in your code, the two string variables will refer to the same, interned string instance.

    You can read some more about it here: Strings in .NET and C# (by Jon Skeet)

    Update
    Just for completeness; as Anthony points out string literals are interned, which can be showed with the following code:

    object firstString = "string1";
    object secondString = "string1";
    Console.WriteLine(firstString == secondString); // prints True
    
    int n = 1;
    object firstString = "string" + n.ToString();
    object secondString = "string" + n.ToString();
    Console.WriteLine(firstString == secondString); // prints False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new in a software development. I have noticed that in some
I'm a relatively new C programmer, and I've noticed that many conventions from other
I am relatively new to .NET/C#/WPF/XAML. I have noticed sometimes controls seem to be
DISCLAIMER: relatively new to Flex/AS3, I might be missing something obvious. After doing some
I'm still relatively new to Rails and have looked through many similar questions here,
I'm relatively new to Threading in Java and I've noticed that everytime I use
I've just noticed this following , relatively new thing on Facebook, when you upload
Im relatively new to the Android world but I have a quick question. I
I am relatively new to using jQuery and I have a problem with creating
Preface Let me start off by saying that I'm a relatively new programmer and

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.