1- using System ;
2- using System . Collections . Generic ;
3-
4- namespace UncomplicatedCustomItems . Extensions
5- {
6- public static class DictionaryExtension
7- {
8- /// <summary>
9- /// Attempts to add a key-value pair to the dictionary. If the key already exists, updates its value.
10- /// </summary>
11- /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
12- /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
13- /// <param name="dictionary">The dictionary to modify.</param>
14- /// <param name="Key">The key to add or update.</param>
15- /// <param name="value">The value to associate with the key.</param>
16- /// <exception cref="ArgumentNullException">Thrown if the dictionary is null.</exception>
17- public static void TryAdd < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , TKey Key , TValue value )
18- {
19- if ( dictionary is null )
20- throw new ArgumentNullException ( nameof ( dictionary ) ) ;
21-
22- if ( dictionary . ContainsKey ( Key ) )
23- dictionary [ Key ] = value ;
24- else
25- dictionary . Add ( Key , value ) ;
26- }
27-
28- /// <summary>
29- /// Attempts to retrieve an element from the dictionary. If the key does not exist, returns a default value.
30- /// </summary>
31- /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
32- /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
33- /// <param name="dictionary">The dictionary to search.</param>
34- /// <param name="key">The key to look for.</param>
35- /// <param name="ifNot">The default value to return if the key is not found.</param>
36- /// <returns>The value associated with the key, or the default value if the key does not exist.</returns>
37- /// <exception cref="ArgumentNullException">Thrown if the dictionary is null.</exception>
38- public static TValue TryGetElement < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , TKey key , TValue ifNot )
39- {
40- if ( dictionary is null )
41- throw new ArgumentNullException ( nameof ( dictionary ) ) ;
42-
43- if ( dictionary . ContainsKey ( key ) )
44- return dictionary [ key ] ;
45-
46- return ifNot ;
47- }
48-
49- /// <summary>
50- /// Attempts to remove a key from the dictionary. If the key does not exist, does nothing.
51- /// </summary>
52- /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
53- /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
54- /// <param name="dictionary">The dictionary to modify.</param>
55- /// <param name="key">The key to remove.</param>
56- /// <exception cref="ArgumentNullException">Thrown if the dictionary is null.</exception>
57- public static void TryRemove < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , TKey key )
58- {
59- if ( dictionary is null )
60- throw new ArgumentNullException ( nameof ( dictionary ) ) ;
61-
62- if ( dictionary . ContainsKey ( key ) )
63- dictionary . Remove ( key ) ;
64- }
65-
66- /// <summary>
67- /// Returns a string representation of the dictionary, including its type and contents.
68- /// </summary>
69- /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
70- /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
71- /// <param name="dictionary">The dictionary to convert to a string.</param>
72- /// <returns>A formatted string representation of the dictionary.</returns>
73- public static string ToRealString < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary )
74- {
75- if ( dictionary is null )
76- return string . Empty ;
77-
78- string Data = $ "[{ dictionary . GetType ( ) . FullName } ] Dictionary<{ dictionary . GetType ( ) . GetGenericArguments ( ) [ 0 ] . FullName } , { dictionary . GetType ( ) . GetGenericArguments ( ) [ 1 ] . FullName } > ({ dictionary . Count } ) [\n ";
79-
80- foreach ( KeyValuePair < TKey , TValue > kvp in dictionary )
81- Data += $ "{ kvp . Key } : { kvp . Value } ,\n ";
82-
83- Data += "];" ;
84-
85- return Data ;
86- }
87-
88- /// <summary>
89- /// Adds a range of key-value pairs to the dictionary.
90- /// </summary>
91- /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
92- /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
93- /// <param name="dictionary">The dictionary to modify.</param>
94- /// <param name="items">A collection of key-value pairs to add.</param>
95- /// <exception cref="ArgumentNullException">Thrown if the dictionary or items is null.</exception>
96- public static void AddRange < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , IEnumerable < KeyValuePair < TKey , TValue > > items )
97- {
98- if ( dictionary is null )
99- throw new ArgumentNullException ( nameof ( dictionary ) ) ;
100-
101- if ( items is null )
102- throw new ArgumentNullException ( nameof ( items ) ) ;
103-
104- foreach ( var kvp in items )
105- {
106- dictionary [ kvp . Key ] = kvp . Value ;
107- }
108- }
109- }
1+ using System ;
2+ using System . Collections . Generic ;
3+
4+ namespace UncomplicatedCustomItems . API . Extensions
5+ {
6+ public static class DictionaryExtension
7+ {
8+ /// <summary>
9+ /// Attempts to add a key-value pair to the dictionary. If the key already exists, updates its value.
10+ /// </summary>
11+ /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
12+ /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
13+ /// <param name="dictionary">The dictionary to modify.</param>
14+ /// <param name="Key">The key to add or update.</param>
15+ /// <param name="value">The value to associate with the key.</param>
16+ /// <exception cref="ArgumentNullException">Thrown if the dictionary is null.</exception>
17+ public static void TryAdd < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , TKey Key , TValue value )
18+ {
19+ if ( dictionary is null )
20+ throw new ArgumentNullException ( nameof ( dictionary ) ) ;
21+
22+ if ( dictionary . ContainsKey ( Key ) )
23+ dictionary [ Key ] = value ;
24+ else
25+ dictionary . Add ( Key , value ) ;
26+ }
27+
28+ /// <summary>
29+ /// Attempts to retrieve an element from the dictionary. If the key does not exist, returns a default value.
30+ /// </summary>
31+ /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
32+ /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
33+ /// <param name="dictionary">The dictionary to search.</param>
34+ /// <param name="key">The key to look for.</param>
35+ /// <param name="ifNot">The default value to return if the key is not found.</param>
36+ /// <returns>The value associated with the key, or the default value if the key does not exist.</returns>
37+ /// <exception cref="ArgumentNullException">Thrown if the dictionary is null.</exception>
38+ public static TValue TryGetElement < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , TKey key , TValue ifNot )
39+ {
40+ if ( dictionary is null )
41+ throw new ArgumentNullException ( nameof ( dictionary ) ) ;
42+
43+ if ( dictionary . ContainsKey ( key ) )
44+ return dictionary [ key ] ;
45+
46+ return ifNot ;
47+ }
48+
49+ /// <summary>
50+ /// Attempts to remove a key from the dictionary. If the key does not exist, does nothing.
51+ /// </summary>
52+ /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
53+ /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
54+ /// <param name="dictionary">The dictionary to modify.</param>
55+ /// <param name="key">The key to remove.</param>
56+ /// <exception cref="ArgumentNullException">Thrown if the dictionary is null.</exception>
57+ public static void TryRemove < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , TKey key )
58+ {
59+ if ( dictionary is null )
60+ throw new ArgumentNullException ( nameof ( dictionary ) ) ;
61+
62+ if ( dictionary . ContainsKey ( key ) )
63+ dictionary . Remove ( key ) ;
64+ }
65+
66+ /// <summary>
67+ /// Returns a string representation of the dictionary, including its type and contents.
68+ /// </summary>
69+ /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
70+ /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
71+ /// <param name="dictionary">The dictionary to convert to a string.</param>
72+ /// <returns>A formatted string representation of the dictionary.</returns>
73+ public static string ToRealString < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary )
74+ {
75+ if ( dictionary is null )
76+ return string . Empty ;
77+
78+ string Data = $ "[{ dictionary . GetType ( ) . FullName } ] Dictionary<{ dictionary . GetType ( ) . GetGenericArguments ( ) [ 0 ] . FullName } , { dictionary . GetType ( ) . GetGenericArguments ( ) [ 1 ] . FullName } > ({ dictionary . Count } ) [\n ";
79+
80+ foreach ( KeyValuePair < TKey , TValue > kvp in dictionary )
81+ Data += $ "{ kvp . Key } : { kvp . Value } ,\n ";
82+
83+ Data += "];" ;
84+
85+ return Data ;
86+ }
87+
88+ /// <summary>
89+ /// Adds a range of key-value pairs to the dictionary.
90+ /// </summary>
91+ /// <typeparam name="TKey">Type of the dictionary key.</typeparam>
92+ /// <typeparam name="TValue">Type of the dictionary value.</typeparam>
93+ /// <param name="dictionary">The dictionary to modify.</param>
94+ /// <param name="items">A collection of key-value pairs to add.</param>
95+ /// <exception cref="ArgumentNullException">Thrown if the dictionary or items is null.</exception>
96+ public static void AddRange < TKey , TValue > ( this Dictionary < TKey , TValue > dictionary , IEnumerable < KeyValuePair < TKey , TValue > > items )
97+ {
98+ if ( dictionary is null )
99+ throw new ArgumentNullException ( nameof ( dictionary ) ) ;
100+
101+ if ( items is null )
102+ throw new ArgumentNullException ( nameof ( items ) ) ;
103+
104+ foreach ( var kvp in items )
105+ {
106+ dictionary [ kvp . Key ] = kvp . Value ;
107+ }
108+ }
109+ }
110110}
0 commit comments