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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:12:45+00:00 2026-06-08T13:12:45+00:00

Employee emp1 = new Employee {Name = Swapnil,Age = 27 }; Employee emp2 =

  • 0
Employee emp1 = new Employee {Name = "Swapnil",Age = 27 };
            Employee emp2 = new Employee { Name = "Swapnil", Age = 27 };
            if (object.Equals(emp1, emp2))
            {

            }
            else
            {
            }

This code is not able to compare.
How i can compare two Object s in C# ?

  • 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-06-08T13:12:47+00:00Added an answer on June 8, 2026 at 1:12 pm

    Without overriding the Equals method only a reference comparison willoccur. You want to perform a value comparison.

    Override the method in your Employee class like so:

    public override bool Equals(Object emp)
    {
        // If parameter is null return false.
        if (emp == null)
        {
            return false;
        }
    
         // If parameter cannot be cast to Point return false.
        Employee e = emp as Employee;
        if ((System.Object)e == null)
        {
            return false;
        }
    
        // Return true if the fields match
        return (Name == emp.Name) && (Age == emp.Age);
    }
    

    Then compare the objects like so:

    if(emp1.Equals(emp2))
    { ... }
    

    OR with the comparison operator:

    if(emp1 == emp2)
    { ... }
    

    More details on MSDN here: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

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

Sidebar

Related Questions

Employee emp = new Employee(); comHandledBySQt.DataSource = emp.GetDataFromTable(1); comHandledBySQt.DisplayMember = FirstName; The above code
Table name: Employee Column Name: Emp_name Emp_name has this contents: xx\rama, xx\rajesh, xx\vignesh I
Suppose I have an Employee object with the following properties: string Name { get;
I can compile the employee example code found on this page . If I
employee = Employee.objects.filter('age' = 99) We assume this queryset is empty. If I use
i have an employee table with fields like id, name, age and salary. Iam
i have an xml <Data> <employee> <name>emp1</name> <id>1</id> </employee> </Data> lets say i have
If I instantiated five objects of Employee class: emp1,emp2...emp5 and put them in a
I have an Employee class object that I am calling from my Login.aspx page's
In my code my child classes are not picking up my parent class for

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.