void WhenTwoValuesAreTheSame_ShouldBeEqual() { var person = new PersonStruct() { FirstName = "John", LastName = "Smith" }; var samePerson = person; Assert.True(person.Equals(samePerson)); Assert.True(person.GetHashCode() == samePerson.GetHashCode()); Assert.True(person == samePerson); Assert.False(person != samePerson); }
void WhenTwoStructsContainsDifferentValues_ShouldNotBeEqual() { var person = new PersonStruct() { FirstName = "John", LastName = "Smith" }; var otherPerson = new PersonStruct() { FirstName = "Bob", LastName = "Smith" };; Assert.False(person.Equals(otherPerson)); Assert.False(person.GetHashCode() == otherPerson.GetHashCode()); Assert.False(person == otherPerson); Assert.True(person != otherPerson); }
static void Main(string[] args) { // Create a normal struct var jane = new PersonStruct() { Name = "Jane", Age = 100 }; // Create a ref struct var bob = new PersonRefStruct() { Name = "Bob", Age = 100 }; // Since a struct is a value type, it can be boxed jane.GetHashCode(); jane.ToString(); // But a ref struct does not alow boxing, so these cause a compler error //bob.GetHashCode(); //bob.ToString(); }
public void GetHashCode_LastNamesNotEqual_NotEqual() { // Arrange var p1 = new PersonStruct { FirstName = "Kim", LastName = "Kuan" }; var p2 = new PersonStruct { FirstName = "Kim", LastName = "Kuann" }; // Act var hash1 = p1.GetHashCode(); var hash2 = p2.GetHashCode(); // Assert Assert.AreNotEqual(hash1, hash2); }
public void CSharpPersonStructTests() { // Arrange var person1 = new PersonStruct("Name", new DateTime(2000, 1, 1)); var person2 = new PersonStruct("Name", new DateTime(2000, 1, 1)); // Debug //output.WriteLine($"person1.GetHashCode(): {person1.GetHashCode()}"); //output.WriteLine($"person2.GetHashCode(): {person2.GetHashCode()}"); // Assert person1.Should().NotBeNull(); person2.Should().NotBeNull(); person1.Should().NotBeSameAs(person2); person1.Should().BeEquivalentTo(person2); person1.Name.Should().NotBeNullOrEmpty(); person1.DateOfBirth.Should().HaveYear(2000); person1.Children.Should().NotBeNull(); Assert.Equal(person1.GetHashCode(), person2.GetHashCode()); Assert.Equal(person1, person2); }