@@ -9,15 +9,15 @@ public class ComponentParameterCollection : ICollection<ComponentParameter>, IRe
9
9
{
10
10
private static readonly MethodInfo CreateTemplateWrapperMethod = GetCreateTemplateWrapperMethod ( ) ;
11
11
private static readonly Type CascadingValueType = typeof ( CascadingValue < > ) ;
12
- private List < ComponentParameter > ? parameters ;
12
+ private readonly List < ComponentParameter > parameters = new ( ) ;
13
13
14
14
/// <summary>
15
15
/// Gets the number of <see cref="ComponentParameter"/> in the collection.
16
16
/// </summary>
17
- public int Count => parameters ? . Count ?? 0 ;
17
+ public int Count => parameters . Count ;
18
18
19
19
/// <inheritdoc />
20
- public bool IsReadOnly { get ; }
20
+ public bool IsReadOnly => false ;
21
21
22
22
/// <summary>
23
23
/// Adds a <paramref name="item"/> to the collection.
@@ -28,9 +28,6 @@ public void Add(ComponentParameter item)
28
28
if ( item . Name is null && item . Value is null )
29
29
throw new ArgumentException ( "A component parameter without a name and value is not valid." , nameof ( item ) ) ;
30
30
31
- if ( parameters is null )
32
- parameters = new List < ComponentParameter > ( ) ;
33
-
34
31
parameters . Add ( item ) ;
35
32
}
36
33
@@ -53,16 +50,16 @@ public void Add(IEnumerable<ComponentParameter> parameters)
53
50
/// </summary>
54
51
/// <param name="item">Parameter to check with.</param>
55
52
/// <returns>True if <paramref name="item"/> is in the collection, false otherwise.</returns>
56
- public bool Contains ( ComponentParameter item ) => parameters ? . Contains ( item ) ?? false ;
53
+ public bool Contains ( ComponentParameter item ) => parameters . Contains ( item ) ;
57
54
58
55
/// <inheritdoc/>
59
- public void Clear ( ) => parameters ? . Clear ( ) ;
56
+ public void Clear ( ) => parameters . Clear ( ) ;
60
57
61
58
/// <inheritdoc/>
62
- public void CopyTo ( ComponentParameter [ ] array , int arrayIndex ) => parameters ? . CopyTo ( array , arrayIndex ) ;
59
+ public void CopyTo ( ComponentParameter [ ] array , int arrayIndex ) => parameters . CopyTo ( array , arrayIndex ) ;
63
60
64
61
/// <inheritdoc/>
65
- public bool Remove ( ComponentParameter item ) => parameters ? . Remove ( item ) ?? false ;
62
+ public bool Remove ( ComponentParameter item ) => parameters . Remove ( item ) ;
66
63
67
64
/// <summary>
68
65
/// Creates a <see cref="RenderFragment"/> that will render a
@@ -108,9 +105,6 @@ void AddComponent(RenderTreeBuilder builder)
108
105
109
106
void AddAttributes ( RenderTreeBuilder builder )
110
107
{
111
- if ( parameters is null )
112
- return ;
113
-
114
108
var attrCount = 100 ;
115
109
116
110
foreach ( var pgroup in parameters . Where ( x => ! x . IsCascadingValue ) . GroupBy ( x => x . Name , StringComparer . Ordinal ) )
@@ -148,7 +142,7 @@ void AddAttributes(RenderTreeBuilder builder)
148
142
149
143
var groupType = groupObject ? . GetType ( ) ;
150
144
151
- if ( groupType != null && groupType . IsGenericType && groupType . GetGenericTypeDefinition ( ) == typeof ( RenderFragment < > ) )
145
+ if ( groupType is { IsGenericType : true } && groupType . GetGenericTypeDefinition ( ) == typeof ( RenderFragment < > ) )
152
146
{
153
147
builder . AddAttribute (
154
148
attrCount ++ ,
@@ -164,49 +158,30 @@ void AddAttributes(RenderTreeBuilder builder)
164
158
165
159
Queue < ( ComponentParameter Parameter , Type Type ) > GetCascadingValues ( )
166
160
{
167
- var cascadingValues = parameters ? . Where ( x => x . IsCascadingValue )
161
+ var cascadingValues = parameters
162
+ . Where ( x => x . IsCascadingValue )
168
163
. Select ( x => ( Parameter : x , Type : GetCascadingValueType ( x ) ) )
169
- . ToArray ( ) ?? Array . Empty < ( ComponentParameter Parameter , Type Type ) > ( ) ;
164
+ . ToArray ( ) ;
170
165
171
- // Detect duplicated unnamed values
172
- for ( var i = 0 ; i < cascadingValues . Length ; i ++ )
166
+ var duplicate = cascadingValues
167
+ . GroupBy ( x => new { x . Type , x . Parameter . Name } )
168
+ . FirstOrDefault ( g => g . Count ( ) > 1 ) ;
169
+
170
+ if ( duplicate is not null )
173
171
{
174
- for ( var j = i + 1 ; j < cascadingValues . Length ; j ++ )
175
- {
176
- if ( cascadingValues [ i ] . Type == cascadingValues [ j ] . Type )
177
- {
178
- var iName = cascadingValues [ i ] . Parameter . Name ;
179
- if ( iName is null )
180
- {
181
- var cascadingValueType = cascadingValues [ i ] . Type . GetGenericArguments ( ) [ 0 ] ;
182
- throw new ArgumentException ( $ "Two or more unnamed cascading values with the type '{ cascadingValueType . Name } ' was added. " +
183
- $ "Only add one unnamed cascading value of the same type.") ;
184
- }
172
+ var name = duplicate . Key . Name ;
173
+ var type = duplicate . First ( ) . Type . GetGenericArguments ( ) [ 0 ] ;
185
174
186
- if ( iName . Equals ( cascadingValues [ j ] . Parameter . Name , StringComparison . Ordinal ) )
187
- {
188
- throw new ArgumentException ( $ "Two or more named cascading values with the name '{ iName } ' and the same type was added. " +
189
- $ "Only add one named cascading value with the same name and type.") ;
190
- }
191
- }
192
- }
175
+ throw new ArgumentException ( $ "Two or more unnamed cascading values with the type '{ name ?? type . Name } ' was added. " +
176
+ $ "Only add one unnamed cascading value of the same type.") ;
193
177
}
194
178
195
179
return new Queue < ( ComponentParameter Parameter , Type Type ) > ( cascadingValues ) ;
196
180
}
197
181
}
198
182
199
183
/// <inheritdoc/>
200
- public IEnumerator < ComponentParameter > GetEnumerator ( )
201
- {
202
- if ( parameters is not null )
203
- {
204
- for ( var i = 0 ; i < parameters . Count ; i ++ )
205
- {
206
- yield return parameters [ i ] ;
207
- }
208
- }
209
- }
184
+ public IEnumerator < ComponentParameter > GetEnumerator ( ) => parameters . GetEnumerator ( ) ;
210
185
211
186
/// <inheritdoc/>
212
187
System . Collections . IEnumerator System . Collections . IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
0 commit comments