Skip to content

Commit fea3732

Browse files
authored
Merge pull request #11 from dotnet-campus/t/lvyi/key-value-pair
Add deconstruct for KeyValuePair
2 parents a1cbce6 + a430326 commit fea3732

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/DotNetCampus.LatestCSharpFeatures.Analyzer/Generators/FeatureGenerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1818

1919
private void OnExecute(IncrementalGeneratorPostInitializationContext context)
2020
{
21+
// .NET Core 3.0 / .NET Standard 2.1 才开始支持 KeyValuePair 的 Deconstruct 方法。
22+
GenerateFeatureSource(context, "KeyValuePair");
23+
2124
// .NET Core 3.0 / .NET Standard 2.1 才开始支持 Nullable;.NET 5.0 开始支持更多。
2225
GenerateFeatureSource(context, "Nullable");
2326

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#nullable enable
2+
namespace System.Collections.Generic;
3+
4+
#if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0_OR_GREATER
5+
#else
6+
/// <summary>
7+
/// Provide extension methods for <see cref="KeyValuePair{TKey,TValue}"/>.
8+
/// </summary>
9+
#if USE_PUBLIC_LATEST_CSHARP_FEATURES
10+
public
11+
#else
12+
internal
13+
#endif
14+
static class DotNetCampusLatestCSharpFeaturesKeyValuePairExtensions
15+
{
16+
#if USE_PUBLIC_LATEST_CSHARP_FEATURES
17+
public
18+
#else
19+
internal
20+
#endif
21+
static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
22+
{
23+
key = pair.Key;
24+
value = pair.Value;
25+
}
26+
}
27+
#endif

0 commit comments

Comments
 (0)