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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:44:34+00:00 2026-05-16T02:44:34+00:00

I have override GetHashCode and Equals and both methods provide same results for different

  • 0

I have override GetHashCode and Equals and both methods provide same results for different objects but why still getting false ?

class Program
{
    static void Main(string[] args)
    {    
        Console.WriteLine(new Person("james") == new Person("james"));    
        Console.ReadKey();
    }    
}

class Person
{
    private string Name;

    public Person(string name)
    {
        Name = name;
    }
    public override int GetHashCode()
    {
        return 1;
    }
    public override bool Equals(object obj)
    {
        return true;
    }
}
  • 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-16T02:44:35+00:00Added an answer on May 16, 2026 at 2:44 am

    Because the == operator defaults to reference equality. It doesn’t call your Equals method.

    You can override the == operator if you want. See: Guidelines for Overriding Equals() and Operator ==

    In C#, there are two different kinds of equality: reference equality (also known as identity) and value equality. Value equality is the generally understood meaning of equality: it means that two objects contain the same values. For example, two integers with the value of 2 have value equality. Reference equality means that there are not two objects to compare. Instead, there are two object references and both of them refer to the same object.

    […]

    By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. It is not a good idea to override operator == in non-immutable types.

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

Sidebar

Related Questions

I've read about 10 different questions on when and how to override GetHashCode but
I have read that when you override Equals on an class/object you need to
From MSDN documentation, it seems as both GetHashCode() and Equals() haven't been overriden in
So I have a class which overrides Equals(object obj) and GetHashCode() along with implementing
I am sure if I override Equals, I need to override GetHashCode as well
Okay, so if I override Equals, I need to override GetHashCode and vice versa.
I need to override Equals() method for one of my types but it seems
I have two classes which both derive from the same parent: public class People{
I'm facing a problem with GetHashCode and Equals which I have overridden for a
I have a class Person for which I have to override the Equals 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.