Object Identity vs. Value Equality

To compare two struct values, use the ValueType.Equals method.

Because all structs implicitly inherit from System.ValueType.

You call the method directly on your object:


using System;

public struct Person
{
    public string Name;
    public int Age;
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

public class Application
{
    static void Main()
    {

        Person p1 = new Person("Jack", 75);
        Person p2;
        p2.Name = "Jack";
        p2.Age = 75;

        if (p2.Equals(p1))
            Console.WriteLine("p2 and p1 have the same values.");

    }
}

The output:


p2 and p1 have the same values.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.