-
Notifications
You must be signed in to change notification settings - Fork 1.9k
C#: Add tests for Equals methods with nullable parameter types
#21342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| using System; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Test | ||
| { | ||
| class TestClass1 : IEquatable<TestClass1> | ||
| { | ||
| private int field1; | ||
|
|
||
| public bool Equals(TestClass1? param1) | ||
| { | ||
| return param1 != null && field1 == param1.field1; | ||
| } | ||
|
|
||
| public override bool Equals(object? param2) | ||
| { | ||
| return param2 is TestClass1 tc && Equals(tc); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return field1; | ||
| } | ||
| } | ||
|
|
||
| class TestClass2 : IEquatable<TestClass2> | ||
| { | ||
| private int field1; | ||
|
|
||
| public bool Equals(TestClass2 param1) | ||
| { | ||
| return param1 != null && field1 == param1.field1; | ||
| } | ||
|
|
||
| public override bool Equals(object? param2) | ||
| { | ||
| return param2 is TestClass2 tc && Equals(tc); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return field1; | ||
| } | ||
| } | ||
|
|
||
| class TestClass3 : IEquatable<TestClass3> | ||
| { | ||
| private int field1; | ||
|
|
||
| public bool Equals(TestClass3? param1) | ||
| { | ||
| return param1 != null && field1 == param1.field1; | ||
| } | ||
|
|
||
| public override bool Equals(object param2) | ||
| { | ||
| return param2 is TestClass3 tc && Equals(tc); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return field1; | ||
| } | ||
| } | ||
|
|
||
| class TestClass4 : IEquatable<TestClass4> | ||
| { | ||
| private int field1; | ||
|
|
||
| public bool Equals(TestClass4 param1) | ||
| { | ||
| return param1 != null && field1 == param1.field1; | ||
hvitved marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public override bool Equals(object param2) | ||
| { | ||
| return param2 is TestClass4 tc && Equals(tc); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return field1; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||
| using System; | ||||||
|
|
||||||
| #nullable enable | ||||||
|
|
||||||
| namespace Test | ||||||
| { | ||||||
| class TestClass1 : IEquatable<TestClass1> | ||||||
| { | ||||||
| private int field1; | ||||||
|
|
||||||
| public bool Equals(TestClass1? param1) | ||||||
| { | ||||||
| return param1 != null && field1 == param1.field1; | ||||||
| } | ||||||
|
|
||||||
| public override bool Equals(object? param2) | ||||||
| { | ||||||
| return param2 is TestClass1 tc && Equals(tc); | ||||||
| } | ||||||
|
|
||||||
| public override int GetHashCode() | ||||||
| { | ||||||
| return field1; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| class TestClass2 : IEquatable<TestClass2> | ||||||
| { | ||||||
| private int field1; | ||||||
|
|
||||||
| public bool Equals(TestClass2 param1) | ||||||
| { | ||||||
| return param1 != null && field1 == param1.field1; | ||||||
|
||||||
| return param1 != null && field1 == param1.field1; | |
| return field1 == param1.field1; |
Copilot
AI
Feb 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The null check param1 != null is ineffective because param1 is declared as non-nullable. With nullable reference types enabled, this parameter cannot be null under normal circumstances, making the null check unnecessary and potentially misleading.
| public bool Equals(TestClass4 param1) | |
| public bool Equals(TestClass4? param1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The null check
param1 != nullis ineffective becauseparam1is declared as non-nullable. With nullable reference types enabled, this parameter cannot be null under normal circumstances, making the null check unnecessary and potentially misleading.