Consider this struct.
public struct myStruct{
public int value1;
public int value2;
public int value3;
public myStruct(int val1, int val2, int val3){
value1 = val1;
value2 = val2;
value3 = val3;
}
}
Now consider that I have some list of type myStruct:
public List<myStruct> myList = new List<myStruct>();
Now I instantiate 2 different versions of myStruct using the same values:
myStruct s1 = new myStruct(1,2,3);
myStruct s2 = new myStruct(1,2,3);
I then add the first instance to myList:
myList.Add(s1);
Then I do a simple check to see if the second instance is in the list:
bool structsEqual = myList.Contains(s2);
Is structsEqual true or false?
Interestingly this specific code will equal true but if the struct contained objects there were not system types, then you would have to implement IComparable for this to not always return false.
For Contains to work you would need to implement IComparable: