Skip to content

ValueObject → record #866

Answered by CapitanMurloc
leotsarev asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @leotsarev,

Example:

public record Address(string Street, string ZipCode);

IComparable interface

Unfortunately, C# records don’t implement the IComparable interface, which means that the following code doesn’t work:

var address1 = new Address("1234 Main St", "20012");
var address2 = new Address("1235 Main St", "20012");
Address[] addresses = new[] { address1, address2 }.OrderBy(x => x).ToArray();

If you try to run it, .NET will throw an exception:

Failed to compare two elements in the array. At least one object must implement IComparable.

C# 9 Records as DDD Value Objects

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by rasmus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #793 on June 11, 2021 13:04.