-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathCollectionExtensions.cs
More file actions
78 lines (66 loc) · 3.13 KB
/
Copy pathCollectionExtensions.cs
File metadata and controls
78 lines (66 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
namespace System.Collections.Generic
{
public static class CollectionExtensions
{
public static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
{
return dictionary.GetValueOrDefault(key, default!);
}
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
ArgumentNullException.ThrowIfNull(dictionary);
TValue? value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;
}
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
ArgumentNullException.ThrowIfNull(dictionary);
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
return true;
}
return false;
}
public static bool Remove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, [MaybeNullWhen(false)] out TValue value)
{
ArgumentNullException.ThrowIfNull(dictionary);
if (dictionary.TryGetValue(key, out value))
{
dictionary.Remove(key);
return true;
}
value = default;
return false;
}
/// <summary>
/// Returns a read-only <see cref="ReadOnlyCollection{T}"/> wrapper
/// for the specified list.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
/// <param name="list">The list to wrap.</param>
/// <returns>An object that acts as a read-only wrapper around the current <see cref="IList{T}"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="list"/> is null.</exception>
public static ReadOnlyCollection<T> AsReadOnly<T>(this IList<T> list)
{
return new ReadOnlyCollection<T>(list);
}
/// <summary>
/// Returns a read-only <see cref="ReadOnlyDictionary{TKey, TValue}"/> wrapper
/// for the current dictionary.
/// </summary>
/// <typeparam name="TKey">The type of keys in the dictionary.</typeparam>
/// <typeparam name="TValue">The type of values in the dictionary.</typeparam>
/// <param name="dictionary">The dictionary to wrap.</param>
/// <returns>An object that acts as a read-only wrapper around the current <see cref="IDictionary{TKey, TValue}"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="dictionary"/> is null.</exception>
public static ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) where TKey : notnull
{
return new ReadOnlyDictionary<TKey, TValue>(dictionary);
}
}
}