Skip to content

Commit c0b91ca

Browse files
Use collection initializers and spreads everywhere
1 parent a6d59a4 commit c0b91ca

File tree

74 files changed

+235
-240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+235
-240
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/AmbiguousApiVersionException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ public partial class AmbiguousApiVersionException : Exception
1212
/// <summary>
1313
/// Initializes a new instance of the <see cref="AmbiguousApiVersionException"/> class.
1414
/// </summary>
15-
public AmbiguousApiVersionException() => apiVersions = Array.Empty<string>();
15+
public AmbiguousApiVersionException() => apiVersions = [];
1616

1717
/// <summary>
1818
/// Initializes a new instance of the <see cref="AmbiguousApiVersionException"/> class.
1919
/// </summary>
2020
/// <param name="message">The associated error message.</param>
2121
public AmbiguousApiVersionException( string message )
22-
: base( message ) => apiVersions = Array.Empty<string>();
22+
: base( message ) => apiVersions = [];
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="AmbiguousApiVersionException"/> class.
2626
/// </summary>
2727
/// <param name="message">The associated error message.</param>
2828
/// <param name="innerException">The inner <see cref="Exception">exception</see> that caused the current exception, if any.</param>
2929
public AmbiguousApiVersionException( string message, Exception innerException )
30-
: base( message, innerException ) => apiVersions = Array.Empty<string>();
30+
: base( message, innerException ) => apiVersions = [];
3131

3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="AmbiguousApiVersionException"/> class.

src/Abstractions/src/Asp.Versioning.Abstractions/Conventions/ApiVersionConventionBuilderBase.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ protected ApiVersionConventionBuilderBase() { }
3939
/// Gets the collection of API versions supported by the current controller.
4040
/// </summary>
4141
/// <value>A <see cref="ICollection{T}">collection</see> of supported <see cref="ApiVersion">API versions</see>.</value>
42-
protected ICollection<ApiVersion> SupportedVersions => supported ??= new();
42+
protected ICollection<ApiVersion> SupportedVersions => supported ??= [];
4343

4444
/// <summary>
4545
/// Gets the collection of API versions deprecated by the current controller.
4646
/// </summary>
4747
/// <value>A <see cref="ICollection{T}">collection</see> of deprecated <see cref="ApiVersion">API versions</see>.</value>
48-
protected ICollection<ApiVersion> DeprecatedVersions => deprecated ??= new();
48+
protected ICollection<ApiVersion> DeprecatedVersions => deprecated ??= [];
4949

5050
/// <summary>
5151
/// Gets the collection of API versions advertised by the current controller.
5252
/// </summary>
5353
/// <value>A <see cref="ICollection{T}">collection</see> of advertised <see cref="ApiVersion">API versions</see>.</value>
54-
protected ICollection<ApiVersion> AdvertisedVersions => advertised ??= new();
54+
protected ICollection<ApiVersion> AdvertisedVersions => advertised ??= [];
5555

5656
/// <summary>
5757
/// Gets the collection of API versions advertised and deprecated by the current controller.
5858
/// </summary>
5959
/// <value>A <see cref="ICollection{T}">collection</see> of advertised and deprecated <see cref="ApiVersion">API versions</see>.</value>
60-
protected ICollection<ApiVersion> DeprecatedAdvertisedVersions => deprecatedAdvertised ??= new();
60+
protected ICollection<ApiVersion> DeprecatedAdvertisedVersions => deprecatedAdvertised ??= [];
6161

6262
/// <summary>
6363
/// Merges API version information from the specified attributes with the current conventions.
@@ -99,19 +99,19 @@ protected virtual void MergeAttributesWithConventions( IReadOnlyList<object> att
9999
switch ( provider.Options )
100100
{
101101
case None:
102-
target = newSupported ??= new();
102+
target = newSupported ??= [];
103103
source = provider.Versions;
104104
break;
105105
case Deprecated:
106-
target = newDeprecated ??= new();
106+
target = newDeprecated ??= [];
107107
source = provider.Versions;
108108
break;
109109
case Advertised:
110-
target = newAdvertised ??= new();
110+
target = newAdvertised ??= [];
111111
source = provider.Versions;
112112
break;
113113
case DeprecatedAdvertised:
114-
target = newDeprecatedAdvertised ??= new();
114+
target = newDeprecatedAdvertised ??= [];
115115
source = provider.Versions;
116116
break;
117117
default:

src/Abstractions/src/Asp.Versioning.Abstractions/LinkHeaderValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public StringSegment Language
8585
#endif
8686
if ( languages is null )
8787
{
88-
languages = new() { value };
88+
languages = [value];
8989
}
9090
else if ( languages.Count == 0 )
9191
{
@@ -105,7 +105,7 @@ public StringSegment Language
105105
/// <remarks>This is only a hint; for example, it does not override the Content-Language header field of
106106
/// a HTTP response obtained by actually following the link. A single link may indicate that multiple
107107
/// languages are available from the indicated resource.</remarks>
108-
public IList<StringSegment> Languages => languages ??= new();
108+
public IList<StringSegment> Languages => languages ??= [];
109109

110110
/// <summary>
111111
/// Gets or sets the link media.
@@ -206,11 +206,11 @@ public static bool TryParse(
206206
type = attribute.Value;
207207
break;
208208
case "hreflang":
209-
languages ??= new();
209+
languages ??= [];
210210
languages.Add( attribute.Value );
211211
break;
212212
default:
213-
extensions ??= new();
213+
extensions ??= [];
214214
extensions.Add( attribute );
215215
break;
216216
}

src/Abstractions/src/Asp.Versioning.Abstractions/NamespaceParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public IReadOnlyList<ApiVersion> Parse( Type type )
9292
}
9393
else if ( versions is null )
9494
{
95-
versions = new() { version, result };
95+
versions = [version, result];
9696
}
9797
else
9898
{

src/Abstractions/test/Asp.Versioning.Abstractions.Tests/IApiVersionParameterSourceExtensionsTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void get_parameter_names_should_return_matching_names()
146146
{
147147
// arrange
148148
var source = new Mock<IApiVersionParameterSource>();
149+
var expected = new[] { "api-version", "ver" };
149150

150151
source.Setup( s => s.AddParameters( It.IsAny<IApiVersionParameterDescriptionContext>() ) )
151152
.Callback( ( IApiVersionParameterDescriptionContext context ) =>
@@ -160,6 +161,6 @@ public void get_parameter_names_should_return_matching_names()
160161
var names = source.Object.GetParameterNames( Query );
161162

162163
// assert
163-
names.Should().BeEquivalentTo( new[] { "api-version", "ver" } );
164+
names.Should().BeEquivalentTo( expected );
164165
}
165166
}

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/ApiExplorer/AdHocEdmScope.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static IReadOnlyList<VersionedApiDescription> FilterResults(
6464
continue;
6565
}
6666

67-
results ??= new();
67+
results ??= [];
6868
results.Add( apiDescription );
6969

7070
for ( var j = 0; j < conventions.Count; j++ )
@@ -73,7 +73,7 @@ private static IReadOnlyList<VersionedApiDescription> FilterResults(
7373
}
7474
}
7575

76-
return results?.ToArray() ?? Array.Empty<VersionedApiDescription>();
76+
return results?.ToArray() ?? [];
7777
}
7878

7979
private static void ApplyAdHocEdm(

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/ApiExplorer/ODataApiExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected override Collection<VersionedApiDescription> ExploreRouteControllers(
181181
return apiDescriptions;
182182
}
183183

184-
apiDescriptions = new();
184+
apiDescriptions = [];
185185
var modelSelector = Configuration.GetODataRootContainer( route ).GetRequiredService<IEdmModelSelector>();
186186
var edmModel = modelSelector.SelectModel( apiVersion );
187187

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/Routing/ODataRouteBuilderContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ private static bool IsNavigationPropertyLink( IEdmEntitySet? entitySet, IEdmSing
355355
{
356356
if ( propertyNames is null )
357357
{
358-
propertyNames = new();
358+
propertyNames = [];
359359
}
360360
else
361361
{

src/AspNet/OData/src/Asp.Versioning.WebApi.OData/OData/EdmModelSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public EdmModelSelector( IEnumerable<IEdmModel> models, IApiVersionSelector apiV
5353

5454
break;
5555
default:
56-
versions = new();
57-
collection = new();
56+
versions = [];
57+
collection = [];
5858

5959
foreach ( var model in models )
6060
{

src/AspNet/OData/src/Asp.Versioning.WebApi.OData/OData/VersionedODataModelBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected virtual IReadOnlyList<ApiVersion> GetApiVersions()
7878

7979
if ( versions.Count > 0 && supported == null )
8080
{
81-
supported = new();
81+
supported = [];
8282
}
8383

8484
for ( var j = 0; j < versions.Count; j++ )
@@ -90,7 +90,7 @@ protected virtual IReadOnlyList<ApiVersion> GetApiVersions()
9090

9191
if ( versions.Count > 0 && deprecated == null )
9292
{
93-
deprecated = new();
93+
deprecated = [];
9494
}
9595

9696
for ( var j = 0; j < versions.Count; j++ )

0 commit comments

Comments
 (0)