Skip to content

Commit 92b904f

Browse files
author
Cam Soper
authored
Fixes #45018 (#45024)
1 parent 85cc37e commit 92b904f

File tree

3 files changed

+7
-64
lines changed

3 files changed

+7
-64
lines changed

docs/core/whats-new/dotnet-10/libraries.md

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,7 @@ public static class StringNormalizationExtensions
7474

7575
Numerical string comparison is a highly requested feature for comparing strings numerically instead of lexicographically. For example, `2` is less than `10`, so `"2"` should appear before `"10"` when ordered numerically. Similarly, `"2"` and `"02"` are equal numerically. With the new `CompareOptions.NumericOrdering` <!--xref:System.Globalization.CompareOptions.NumericOrdering--> option, it's now possible to do these types of comparisons:
7676

77-
<!-- >:::code language="csharp" source="../snippets/dotnet-10/csharp/snippets.cs" id="snippet1"::: -->
78-
79-
```csharp
80-
StringComparer numericStringComparer = StringComparer.Create(CultureInfo.CurrentCulture, CompareOptions.NumericOrdering);
81-
82-
Console.WriteLine(numericStringComparer.Equals("02", "2"));
83-
// Output: True
84-
85-
foreach (string os in new[] { "Windows 8", "Windows 10", "Windows 11" }.Order(numericStringComparer))
86-
{
87-
Console.WriteLine(os);
88-
}
89-
90-
// Output:
91-
// Windows 8
92-
// Windows 10
93-
// Windows 11
94-
95-
HashSet<string> set = new HashSet<string>(numericStringComparer) { "007" };
96-
Console.WriteLine(set.Contains("7"));
97-
// Output: True
98-
```
77+
:::code language="csharp" source="../snippets/dotnet-10/csharp/snippets.cs" id="snippet_numericOrdering":::
9978

10079
Note that this option is not valid for the following index-based string operations: `IndexOf`, `LastIndexOf`, `StartsWith`, `EndsWith`, `IsPrefix`, and `IsSuffix`.
10180

@@ -142,47 +121,15 @@ public class OrderedDictionary<TKey, TValue>
142121

143122
This index can then be used with <xref:System.Collections.Generic.OrderedDictionary`2.GetAt*>/<xref:System.Collections.Generic.OrderedDictionary`2.SetAt*> for fast access to the entry. An example usage of the new `TryAdd` overload is to add or update a key/value pair in the ordered dictionary:
144123

145-
<!-- :::code language="csharp" source="../snippets/dotnet-10/csharp/snippets.cs" id="snippet2"::: -->
146-
147-
```csharp
148-
// Try to add a new key with value 1.
149-
if (!orderedDictionary.TryAdd(key, 1, out int index))
150-
{
151-
// Key was present, so increment the existing value instead.
152-
int value = orderedDictionary.GetAt(index).Value;
153-
orderedDictionary.SetAt(index, value + 1);
154-
}
155-
```
124+
:::code language="csharp" source="../snippets/dotnet-10/csharp/snippets.cs" id="snippet_getAtSetAt":::
156125

157126
This new API is already used in <xref:System.Json.JsonObject> and improves the performance of updating properties by 10-20%.
158127

159128
## Allow specifying ReferenceHandler in `JsonSourceGenerationOptions`
160129

161130
When using source generators for JSON serialization, the generated context will throw when cycles are serialized or deserialized. This behavior can now be customized by specifying the <xref:System.Text.Json.Serialization.ReferenceHandler> in the <xref:System.Text.Json.Serialization.JsonSourceGenerationOptionsAttribute>. Here is an example using `JsonKnownReferenceHandler.Preserve`:
162131

163-
<!-- :::code language="csharp" source="../snippets/dotnet-10/csharp/snippets.cs" id="snippet3"::: -->
164-
165-
```csharp
166-
public static void MakeSelfRef()
167-
{
168-
SelfReference selfRef = new SelfReference();
169-
selfRef.Me = selfRef;
170-
171-
Console.WriteLine(JsonSerializer.Serialize(selfRef, ContextWithPreserveReference.Default.SelfReference));
172-
// Output: {"$id":"1","Me":{"$ref":"1"}}
173-
}
174-
175-
[JsonSourceGenerationOptions(ReferenceHandler = JsonKnownReferenceHandler.Preserve)]
176-
[JsonSerializable(typeof(SelfReference))]
177-
internal partial class ContextWithPreserveReference : JsonSerializerContext
178-
{
179-
}
180-
181-
internal class SelfReference
182-
{
183-
public SelfReference Me { get; set; } = null!;
184-
}
185-
```
132+
:::code language="csharp" source="../snippets/dotnet-10/csharp/snippets.cs" id="snippet_selfReference":::
186133

187134
## More left-handed matrix transformation methods
188135

docs/core/whats-new/snippets/dotnet-10/csharp/Project.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

docs/core/whats-new/snippets/dotnet-10/csharp/snippets.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
public static class Snippets
66
{
7-
/*
87
public static void NumericOrdering()
98
{
10-
#region snippet1
9+
#region snippet_numericOrdering
1110
StringComparer numericStringComparer = StringComparer.Create(CultureInfo.CurrentCulture, CompareOptions.NumericOrdering);
1211

1312
Console.WriteLine(numericStringComparer.Equals("02", "2"));
@@ -31,7 +30,7 @@ public static void NumericOrdering()
3130

3231
public static void IncrementValue(OrderedDictionary<string, int> orderedDictionary, string key)
3332
{
34-
#region snippet2
33+
#region snippet_getAtSetAt
3534
// Try to add a new key with value 1.
3635
if (!orderedDictionary.TryAdd(key, 1, out int index))
3736
{
@@ -41,14 +40,12 @@ public static void IncrementValue(OrderedDictionary<string, int> orderedDictiona
4140
}
4241
#endregion
4342
}
44-
*/
4543
}
4644

4745

4846
public partial class SomeClass
4947
{
50-
/*
51-
#region snippet3
48+
#region snippet_selfReference
5249
public static void MakeSelfRef()
5350
{
5451
SelfReference selfRef = new SelfReference();
@@ -69,6 +66,5 @@ internal class SelfReference
6966
public SelfReference Me { get; set; } = null!;
7067
}
7168
#endregion
72-
*/
7369
}
7470

0 commit comments

Comments
 (0)