Mega Code Archive
0153 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.