|
| 1 | +--- |
| 2 | +title: "Breaking change: User info in `mailto:` URIs is compared" |
| 3 | +description: Learn about the .NET 8 breaking change in networking where URI comparison now considers user info for `mailto:` URIs. |
| 4 | +ms.date: 10/03/2024 |
| 5 | +--- |
| 6 | +# User info in `mailto:` URIs is compared |
| 7 | + |
| 8 | +Previously, <xref:System.Uri> didn't compare user info when comparing two `Uri` instances for equality. However, this behavior is not intuitive in the case of `mailto:` URIs. With this change, <xref:System.Uri.Equals*?displayProperty=nameWithType> and the [`==`](xref:System.Uri.op_Equality(System.Uri,System.Uri)) operator now consider user info when comparing URIs. |
| 9 | + |
| 10 | +## Previous behavior |
| 11 | + |
| 12 | +Prior to .NET 8, both of the following comparisons returned `true`. |
| 13 | + |
| 14 | +```csharp |
| 15 | +Uri uri1 = new Uri( "https://[email protected]"); |
| 16 | +Uri uri2 = new Uri( "https://[email protected]"); |
| 17 | +System.Console.WriteLine(uri1 == uri2); // True. |
| 18 | +
|
| 19 | +Uri uri3 = new Uri( "mailto:[email protected]"); |
| 20 | +Uri uri4 = new Uri( "mailto:[email protected]"); |
| 21 | +System.Console.WriteLine(uri3 == uri4); // True. |
| 22 | +``` |
| 23 | + |
| 24 | +## New behavior |
| 25 | + |
| 26 | +Starting in .NET 8, the first comparison still returns `true`, but the second comparison (of `mailto` URIs) returns `false`. |
| 27 | + |
| 28 | +```csharp |
| 29 | +Uri uri1 = new Uri( "https://[email protected]"); |
| 30 | +Uri uri2 = new Uri( "https://[email protected]"); |
| 31 | +System.Console.WriteLine(uri1 == uri2); // True. |
| 32 | +
|
| 33 | +Uri uri3 = new Uri( "mailto:[email protected]"); |
| 34 | +Uri uri4 = new Uri( "mailto:[email protected]"); |
| 35 | +System.Console.WriteLine(uri3 == uri4); // False. |
| 36 | +``` |
| 37 | + |
| 38 | +## Version introduced |
| 39 | + |
| 40 | +.NET 8 |
| 41 | + |
| 42 | +## Type of breaking change |
| 43 | + |
| 44 | +This change is a [behavioral change](../../categories.md#behavioral-change). |
| 45 | + |
| 46 | +## Reason for change |
| 47 | + |
| 48 | +The previous behavior was unexpected and unintuitive. |
| 49 | + |
| 50 | +## Recommended action |
| 51 | + |
| 52 | +If you want to compare only the host part of email addresses, compare only the <xref:System.Uri.Host?displayProperty=nameWithType> members. |
| 53 | + |
| 54 | +## Affected APIs |
| 55 | + |
| 56 | +- <xref:System.Uri.Equals(System.Uri)?displayProperty=fullName> |
| 57 | +- <xref:System.Uri.Equals(System.Object)?displayProperty=fullName> |
| 58 | +- <xref:System.Uri.op_Equality(System.Uri,System.Uri)?displayProperty=fullName> |
0 commit comments