-
Notifications
You must be signed in to change notification settings - Fork 6k
Update value equality article with records examples, detailed justifications, and improved formatting #48010
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f901d47
Initial plan
Copilot 25554b7
Update value equality article with records example and detailed justi…
Copilot 9ce6b61
Apply suggestions from code review
BillWagner b0dc7bc
Address review comments: simplify records and fix formatting
Copilot 79f5d4a
Apply suggestions from code review
BillWagner a78bf32
Convert records recommendation to TIP block format
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...operators/snippets/how-to-define-value-equality-for-a-type/ValueEqualityRecord/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
namespace ValueEqualityRecord; | ||
|
||
public record TwoDPoint(int X, int Y); | ||
|
||
public record ThreeDPoint(int X, int Y, int Z) : TwoDPoint(X, Y); | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
// Create some points | ||
TwoDPoint pointA = new TwoDPoint(3, 4); | ||
TwoDPoint pointB = new TwoDPoint(3, 4); | ||
TwoDPoint pointC = new TwoDPoint(5, 6); | ||
|
||
ThreeDPoint point3D_A = new ThreeDPoint(3, 4, 5); | ||
ThreeDPoint point3D_B = new ThreeDPoint(3, 4, 5); | ||
ThreeDPoint point3D_C = new ThreeDPoint(3, 4, 7); | ||
|
||
Console.WriteLine("=== Value Equality with Records ==="); | ||
|
||
// Value equality works automatically | ||
Console.WriteLine($"pointA.Equals(pointB) = {pointA.Equals(pointB)}"); // True | ||
Console.WriteLine($"pointA == pointB = {pointA == pointB}"); // True | ||
Console.WriteLine($"pointA.Equals(pointC) = {pointA.Equals(pointC)}"); // False | ||
Console.WriteLine($"pointA == pointC = {pointA == pointC}"); // False | ||
|
||
Console.WriteLine("\n=== Hash Codes ==="); | ||
|
||
// Equal objects have equal hash codes automatically | ||
Console.WriteLine($"pointA.GetHashCode() = {pointA.GetHashCode()}"); | ||
Console.WriteLine($"pointB.GetHashCode() = {pointB.GetHashCode()}"); | ||
Console.WriteLine($"pointC.GetHashCode() = {pointC.GetHashCode()}"); | ||
|
||
Console.WriteLine("\n=== Inheritance with Records ==="); | ||
|
||
// Inheritance works correctly with value equality | ||
Console.WriteLine($"point3D_A.Equals(point3D_B) = {point3D_A.Equals(point3D_B)}"); // True | ||
Console.WriteLine($"point3D_A == point3D_B = {point3D_A == point3D_B}"); // True | ||
Console.WriteLine($"point3D_A.Equals(point3D_C) = {point3D_A.Equals(point3D_C)}"); // False | ||
|
||
// Different types are not equal (unlike problematic class example) | ||
Console.WriteLine($"pointA.Equals(point3D_A) = {pointA.Equals(point3D_A)}"); // False | ||
|
||
Console.WriteLine("\n=== Collections ==="); | ||
|
||
// Works seamlessly with collections | ||
var pointSet = new HashSet<TwoDPoint> { pointA, pointB, pointC }; | ||
Console.WriteLine($"Set contains {pointSet.Count} unique points"); // 2 unique points | ||
|
||
var pointDict = new Dictionary<TwoDPoint, string> | ||
{ | ||
{ pointA, "First point" }, | ||
{ pointC, "Different point" } | ||
}; | ||
|
||
// Demonstrate that equivalent points work as the same key | ||
var duplicatePoint = new TwoDPoint(3, 4); | ||
Console.WriteLine($"Dictionary contains key for {duplicatePoint}: {pointDict.ContainsKey(duplicatePoint)}"); // True | ||
Console.WriteLine($"Dictionary contains {pointDict.Count} entries"); // 2 entries | ||
|
||
Console.WriteLine("\n=== String Representation ==="); | ||
|
||
// Automatic ToString implementation | ||
Console.WriteLine($"pointA.ToString() = {pointA}"); | ||
Console.WriteLine($"point3D_A.ToString() = {point3D_A}"); | ||
|
||
Console.WriteLine("Press any key to exit."); | ||
Console.ReadKey(); | ||
} | ||
} | ||
|
||
/* Expected Output: | ||
=== Value Equality with Records === | ||
pointA.Equals(pointB) = True | ||
pointA == pointB = True | ||
pointA.Equals(pointC) = False | ||
pointA == pointC = False | ||
|
||
=== Hash Codes === | ||
pointA.GetHashCode() = -1400834708 | ||
pointB.GetHashCode() = -1400834708 | ||
pointC.GetHashCode() = -148136000 | ||
|
||
=== Inheritance with Records === | ||
point3D_A.Equals(point3D_B) = True | ||
point3D_A == point3D_B = True | ||
point3D_A.Equals(point3D_C) = False | ||
pointA.Equals(point3D_A) = False | ||
|
||
=== Collections === | ||
Set contains 2 unique points | ||
Dictionary contains key for TwoDPoint { X = 3, Y = 4 }: True | ||
Dictionary contains 2 entries | ||
|
||
=== String Representation === | ||
pointA.ToString() = TwoDPoint { X = 3, Y = 4 } | ||
point3D_A.ToString() = ThreeDPoint { X = 3, Y = 4, Z = 5 } | ||
*/ |
10 changes: 10 additions & 0 deletions
10
...ts/how-to-define-value-equality-for-a-type/ValueEqualityRecord/ValueEqualityRecord.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
</Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.