Skip to content

Commit f978740

Browse files
committed
Enabled C# 12 features
- Collection initialization can be simplified
1 parent 8d22a33 commit f978740

5 files changed

Lines changed: 40 additions & 40 deletions

File tree

src/Skybrud.Essentials.AspNetCore/FormCollectionExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static bool TryGetInt32(this IFormCollection? formData, string key, [NotN
199199
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="int"/>
200200
/// value will be ignored.</remarks>
201201
public static int[] GetInt32Array(this IFormCollection? formData, string key) {
202-
return formData == null ? Array.Empty<int>() : formData[key].ToInt32Array();
202+
return formData == null ? [] : formData[key].ToInt32Array();
203203
}
204204

205205
/// <summary>
@@ -212,7 +212,7 @@ public static int[] GetInt32Array(this IFormCollection? formData, string key) {
212212
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="int"/>
213213
/// value will be ignored.</remarks>
214214
public static List<int> GetInt32List(this IFormCollection? formData, string key) {
215-
return formData?[key].ToInt32List() ?? new List<int>();
215+
return formData?[key].ToInt32List() ?? [];
216216
}
217217

218218
#endregion
@@ -291,7 +291,7 @@ public static bool TryGetInt64(this IFormCollection? formData, string key, [NotN
291291
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="long"/>
292292
/// value will be ignored.</remarks>
293293
public static long[] GetInt64Array(this IFormCollection? formData, string key) {
294-
return formData == null ? Array.Empty<long>() : formData[key].ToInt64Array();
294+
return formData == null ? [] : formData[key].ToInt64Array();
295295
}
296296

297297
/// <summary>
@@ -304,7 +304,7 @@ public static long[] GetInt64Array(this IFormCollection? formData, string key) {
304304
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="long"/>
305305
/// value will be ignored.</remarks>
306306
public static List<long> GetInt64List(this IFormCollection? formData, string key) {
307-
return formData?[key].ToInt64List() ?? new List<long>();
307+
return formData?[key].ToInt64List() ?? [];
308308
}
309309

310310
#endregion
@@ -383,7 +383,7 @@ public static bool TryGetFloat(this IFormCollection? formData, string key, [NotN
383383
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="float"/>
384384
/// value will be ignored.</remarks>
385385
public static float[] GetFloatArray(this IFormCollection? formData, string key) {
386-
return formData?[key].ToFloatArray() ?? Array.Empty<float>();
386+
return formData?[key].ToFloatArray() ?? [];
387387
}
388388

389389
/// <summary>
@@ -396,7 +396,7 @@ public static float[] GetFloatArray(this IFormCollection? formData, string key)
396396
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="float"/>
397397
/// value will be ignored.</remarks>
398398
public static List<float> GetFloatList(this IFormCollection? formData, string key) {
399-
return formData?[key].ToFloatList() ?? new List<float>();
399+
return formData?[key].ToFloatList() ?? [];
400400
}
401401

402402
#endregion
@@ -475,7 +475,7 @@ public static bool TryGetDouble(this IFormCollection? formData, string key, [Not
475475
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="double"/>
476476
/// value will be ignored.</remarks>
477477
public static double[] GetDoubleArray(this IFormCollection? formData, string key) {
478-
return formData == null ? Array.Empty<double>() : formData[key].ToDoubleArray();
478+
return formData == null ? [] : formData[key].ToDoubleArray();
479479
}
480480

481481
/// <summary>
@@ -488,7 +488,7 @@ public static double[] GetDoubleArray(this IFormCollection? formData, string key
488488
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="double"/>
489489
/// value will be ignored.</remarks>
490490
public static List<double> GetDoubleList(this IFormCollection? formData, string key) {
491-
return formData?[key].ToDoubleList() ?? new List<double>();
491+
return formData?[key].ToDoubleList() ?? [];
492492
}
493493

494494
#endregion
@@ -608,7 +608,7 @@ public static Guid GetGuid(this IFormCollection? formData, string key, Guid fall
608608
/// <param name="key">The key of the form data component.</param>
609609
/// <returns>An instance of <see cref="List{Guid}"/>.</returns>
610610
public static Guid[] GetGuidArray(this IFormCollection? formData, string key) {
611-
return formData?[key].ToGuidArray() ?? Array.Empty<Guid>();
611+
return formData?[key].ToGuidArray() ?? [];
612612
}
613613

614614
/// <summary>
@@ -618,7 +618,7 @@ public static Guid[] GetGuidArray(this IFormCollection? formData, string key) {
618618
/// <param name="key">The key of the form data component.</param>
619619
/// <returns>An instance of <see cref="List{Guid}"/>.</returns>
620620
public static List<Guid> GetGuidList(this IFormCollection? formData, string key) {
621-
return formData?[key].ToGuidList() ?? new List<Guid>();
621+
return formData?[key].ToGuidList() ?? [];
622622
}
623623

624624
/// <summary>

src/Skybrud.Essentials.AspNetCore/Json/Newtonsoft/Attributes/NewtonsoftJsonOnlyConfigurationAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public NewtonsoftJsonOnlyConfigurationAttribute(TextCasing casing, Formatting fo
2626
Casing = casing;
2727
Formatting = formatting;
2828
Order = 1;
29-
Arguments = new object[] { this };
29+
Arguments = [this];
3030
}
3131

3232
}

src/Skybrud.Essentials.AspNetCore/QueryStringExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static bool TryGetInt32(this IQueryCollection? query, string key, [NotNul
201201
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="int"/>
202202
/// value will be ignored.</remarks>
203203
public static int[] GetInt32Array(this IQueryCollection? query, string key) {
204-
return query == null ? Array.Empty<int>() : query[key].ToInt32Array();
204+
return query == null ? [] : query[key].ToInt32Array();
205205
}
206206

207207
/// <summary>
@@ -214,7 +214,7 @@ public static int[] GetInt32Array(this IQueryCollection? query, string key) {
214214
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="int"/>
215215
/// value will be ignored.</remarks>
216216
public static List<int> GetInt32List(this IQueryCollection? query, string key) {
217-
return query?[key].ToInt32List() ?? new List<int>();
217+
return query?[key].ToInt32List() ?? [];
218218
}
219219

220220
#endregion
@@ -293,7 +293,7 @@ public static bool TryGetInt64(this IQueryCollection? query, string key, [NotNul
293293
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="long"/>
294294
/// value will be ignored.</remarks>
295295
public static long[] GetInt64Array(this IQueryCollection? query, string key) {
296-
return query == null ? Array.Empty<long>() : query[key].ToInt64Array();
296+
return query == null ? [] : query[key].ToInt64Array();
297297
}
298298

299299
/// <summary>
@@ -306,7 +306,7 @@ public static long[] GetInt64Array(this IQueryCollection? query, string key) {
306306
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="long"/>
307307
/// value will be ignored.</remarks>
308308
public static List<long> GetInt64List(this IQueryCollection? query, string key) {
309-
return query?[key].ToInt64List() ?? new List<long>();
309+
return query?[key].ToInt64List() ?? [];
310310
}
311311

312312
#endregion
@@ -385,7 +385,7 @@ public static bool TryGetFloat(this IQueryCollection? query, string key, [NotNul
385385
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="float"/>
386386
/// value will be ignored.</remarks>
387387
public static float[] GetFloatArray(this IQueryCollection? query, string key) {
388-
return query?[key].ToFloatArray() ?? Array.Empty<float>();
388+
return query?[key].ToFloatArray() ?? [];
389389
}
390390

391391
/// <summary>
@@ -398,7 +398,7 @@ public static float[] GetFloatArray(this IQueryCollection? query, string key) {
398398
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="float"/>
399399
/// value will be ignored.</remarks>
400400
public static List<float> GetFloatList(this IQueryCollection? query, string key) {
401-
return query?[key].ToFloatList() ?? new List<float>();
401+
return query?[key].ToFloatList() ?? [];
402402
}
403403

404404
#endregion
@@ -477,7 +477,7 @@ public static bool TryGetDouble(this IQueryCollection? query, string key, [NotNu
477477
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="double"/>
478478
/// value will be ignored.</remarks>
479479
public static double[] GetDoubleArray(this IQueryCollection? query, string key) {
480-
return query == null ? Array.Empty<double>() : query[key].ToDoubleArray();
480+
return query == null ? [] : query[key].ToDoubleArray();
481481
}
482482

483483
/// <summary>
@@ -490,7 +490,7 @@ public static double[] GetDoubleArray(this IQueryCollection? query, string key)
490490
/// values - e.g. separated by commas. Values that can not be converted to a corresponding <see cref="double"/>
491491
/// value will be ignored.</remarks>
492492
public static List<double> GetDoubleList(this IQueryCollection? query, string key) {
493-
return query?[key].ToDoubleList() ?? new List<double>();
493+
return query?[key].ToDoubleList() ?? [];
494494
}
495495

496496
#endregion
@@ -610,7 +610,7 @@ public static Guid GetGuid(this IQueryCollection? query, string key, Guid fallba
610610
/// <param name="key">The key of the query string component.</param>
611611
/// <returns>An instance of <see cref="List{Guid}"/>.</returns>
612612
public static Guid[] GetGuidArray(this IQueryCollection? query, string key) {
613-
return query?[key].ToGuidArray() ?? Array.Empty<Guid>();
613+
return query?[key].ToGuidArray() ?? [];
614614
}
615615

616616
/// <summary>
@@ -620,7 +620,7 @@ public static Guid[] GetGuidArray(this IQueryCollection? query, string key) {
620620
/// <param name="key">The key of the query string component.</param>
621621
/// <returns>An instance of <see cref="List{Guid}"/>.</returns>
622622
public static List<Guid> GetGuidList(this IQueryCollection? query, string key) {
623-
return query?[key].ToGuidList() ?? new List<Guid>();
623+
return query?[key].ToGuidList() ?? [];
624624
}
625625

626626
/// <summary>

src/Skybrud.Essentials.AspNetCore/Skybrud.Essentials.AspNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<LangVersion>11.0</LangVersion>
4+
<LangVersion>12.0</LangVersion>
55
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
66
<Nullable>enable</Nullable>
77
<CheckEolTargetFramework>false</CheckEolTargetFramework>

src/Skybrud.Essentials.AspNetCore/StringValuesExtensions.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Skybrud.Essentials.AspNetCore;
1515
/// </summary>
1616
public static class StringValuesExtensions {
1717

18-
internal static readonly char[] DefaultSeparators = { ',', ' ', '\r', '\n', '\t' };
18+
internal static readonly char[] DefaultSeparators = [',', ' ', '\r', '\n', '\t'];
1919

2020
#region ToString...
2121

@@ -25,7 +25,7 @@ public static class StringValuesExtensions {
2525
/// <param name="values">The string values.</param>
2626
/// <returns>An array of <see cref="string"/>.</returns>
2727
public static string[] ToStringArray(this StringValues? values) {
28-
return values?.SelectMany(StringUtils.ParseStringArray).ToArray() ?? Array.Empty<string>();
28+
return values?.SelectMany(StringUtils.ParseStringArray).ToArray() ?? [];
2929
}
3030

3131
/// <summary>
@@ -35,7 +35,7 @@ public static string[] ToStringArray(this StringValues? values) {
3535
/// <param name="separators">An array of supported separators.</param>
3636
/// <returns>An array of <see cref="string"/>.</returns>
3737
public static string[] ToStringArray(this StringValues? values, params char[] separators) {
38-
return values?.SelectMany(x => StringUtils.ParseStringArray(x, separators)).ToArray() ?? Array.Empty<string>();
38+
return values?.SelectMany(x => StringUtils.ParseStringArray(x, separators)).ToArray() ?? [];
3939
}
4040

4141
/// <summary>
@@ -44,7 +44,7 @@ public static string[] ToStringArray(this StringValues? values, params char[] se
4444
/// <param name="values">The string values.</param>
4545
/// <returns>A list of <see cref="string"/>.</returns>
4646
public static List<string> ToStringList(this StringValues? values) {
47-
return values?.SelectMany(StringUtils.ParseStringArray).ToList() ?? new List<string>();
47+
return values?.SelectMany(StringUtils.ParseStringArray).ToList() ?? [];
4848
}
4949

5050
/// <summary>
@@ -54,7 +54,7 @@ public static List<string> ToStringList(this StringValues? values) {
5454
/// <param name="separators">An array of supported separators.</param>
5555
/// <returns>A list of <see cref="string"/>.</returns>
5656
public static List<string> ToStringList(this StringValues? values, params char[] separators) {
57-
return values?.SelectMany(x => StringUtils.ParseStringArray(x, separators)).ToList() ?? new List<string>();
57+
return values?.SelectMany(x => StringUtils.ParseStringArray(x, separators)).ToList() ?? [];
5858
}
5959

6060
#endregion
@@ -100,7 +100,7 @@ public static int ToInt32(this StringValues values, int fallback) {
100100
/// <param name="values">The string values.</param>
101101
/// <returns>An array of <see cref="int"/>.</returns>
102102
public static int[] ToInt32Array(this StringValues values) {
103-
return values.SelectMany(StringUtils.ParseInt32Array).ToArray();
103+
return [.. values.SelectMany(StringUtils.ParseInt32Array)];
104104
}
105105

106106
/// <summary>
@@ -109,7 +109,7 @@ public static int[] ToInt32Array(this StringValues values) {
109109
/// <param name="values">The string values.</param>
110110
/// <returns>A list of <see cref="int"/>.</returns>
111111
public static List<int> ToInt32List(this StringValues values) {
112-
return values.SelectMany(StringUtils.ParseInt32List).ToList();
112+
return [.. values.SelectMany(StringUtils.ParseInt32List)];
113113
}
114114

115115
#endregion
@@ -155,7 +155,7 @@ public static long ToInt64(this StringValues values, long fallback) {
155155
/// <param name="values">The string values.</param>
156156
/// <returns>An array of <see cref="long"/>.</returns>
157157
public static long[] ToInt64Array(this StringValues values) {
158-
return values.SelectMany(StringUtils.ParseInt64Array).ToArray();
158+
return [.. values.SelectMany(StringUtils.ParseInt64Array)];
159159
}
160160

161161
/// <summary>
@@ -164,7 +164,7 @@ public static long[] ToInt64Array(this StringValues values) {
164164
/// <param name="values">The string values.</param>
165165
/// <returns>A list of <see cref="long"/>.</returns>
166166
public static List<long> ToInt64List(this StringValues values) {
167-
return values.SelectMany(StringUtils.ParseInt64List).ToList();
167+
return [.. values.SelectMany(StringUtils.ParseInt64List)];
168168
}
169169

170170
#endregion
@@ -210,7 +210,7 @@ public static float ToFloat(this StringValues values, float fallback) {
210210
/// <param name="values">The string values.</param>
211211
/// <returns>An array of <see cref="float"/>.</returns>
212212
public static float[] ToFloatArray(this StringValues values) {
213-
return values.SelectMany(StringUtils.ParseFloatArray).ToArray();
213+
return [.. values.SelectMany(StringUtils.ParseFloatArray)];
214214
}
215215

216216
/// <summary>
@@ -219,7 +219,7 @@ public static float[] ToFloatArray(this StringValues values) {
219219
/// <param name="values">The string values.</param>
220220
/// <returns>A list of <see cref="float"/>.</returns>
221221
public static List<float> ToFloatList(this StringValues values) {
222-
return values.SelectMany(StringUtils.ParseFloatList).ToList();
222+
return [.. values.SelectMany(StringUtils.ParseFloatList)];
223223
}
224224

225225
#endregion
@@ -265,7 +265,7 @@ public static double ToDouble(this StringValues values, double fallback) {
265265
/// <param name="values">The string values.</param>
266266
/// <returns>An array of <see cref="double"/>.</returns>
267267
public static double[] ToDoubleArray(this StringValues values) {
268-
return values.SelectMany(StringUtils.ParseDoubleArray).ToArray();
268+
return [.. values.SelectMany(StringUtils.ParseDoubleArray)];
269269
}
270270

271271
/// <summary>
@@ -274,7 +274,7 @@ public static double[] ToDoubleArray(this StringValues values) {
274274
/// <param name="values">The string values.</param>
275275
/// <returns>A list of <see cref="double"/>.</returns>
276276
public static List<double> ToDoubleList(this StringValues values) {
277-
return values.SelectMany(StringUtils.ParseDoubleList).ToList();
277+
return [.. values.SelectMany(StringUtils.ParseDoubleList)];
278278
}
279279

280280
#endregion
@@ -356,7 +356,7 @@ public static Guid ToGuid(this StringValues values, Guid fallback) {
356356
/// <param name="values">The string values.</param>
357357
/// <returns>An array of <see cref="Guid"/>.</returns>
358358
public static Guid[] ToGuidArray(this StringValues values) {
359-
return values.SelectMany(StringUtils.ParseGuidArray).ToArray();
359+
return [.. values.SelectMany(StringUtils.ParseGuidArray)];
360360
}
361361

362362
/// <summary>
@@ -365,7 +365,7 @@ public static Guid[] ToGuidArray(this StringValues values) {
365365
/// <param name="values">The string values.</param>
366366
/// <returns>A list of <see cref="Guid"/>.</returns>
367367
public static List<Guid> ToGuidList(this StringValues values) {
368-
return values.SelectMany(StringUtils.ParseGuidList).ToList();
368+
return [.. values.SelectMany(StringUtils.ParseGuidList)];
369369
}
370370

371371
#endregion
@@ -411,7 +411,7 @@ public static TEnum ToEnum<TEnum>(this StringValues? values, TEnum fallback) whe
411411
/// <param name="values">The string values.</param>
412412
/// <returns>An array of <typeparamref name="TEnum"/>.</returns>
413413
public static TEnum[] ToEnumArray<TEnum>(this StringValues? values) where TEnum : struct, Enum {
414-
return values is { Count: > 0 } ? ToEnumList<TEnum>(values).ToArray() : Array.Empty<TEnum>();
414+
return values is { Count: > 0 } ? [.. ToEnumList<TEnum>(values)] : [];
415415
}
416416

417417
/// <summary>
@@ -421,7 +421,7 @@ public static TEnum[] ToEnumArray<TEnum>(this StringValues? values) where TEnum
421421
/// <param name="separators">An array of supported separators.</param>
422422
/// <returns>An array of <typeparamref name="TEnum"/>.</returns>
423423
public static TEnum[] ToEnumArray<TEnum>(this StringValues? values, params char[] separators) where TEnum : struct, Enum {
424-
return values is { Count: > 0 } ? ToEnumList<TEnum>(values, separators).ToArray() : Array.Empty<TEnum>();
424+
return values is { Count: > 0 } ? [.. ToEnumList<TEnum>(values, separators)] : [];
425425
}
426426

427427
/// <summary>
@@ -441,7 +441,7 @@ public static List<TEnum> ToEnumList<TEnum>(this StringValues? values) where TEn
441441
/// <returns>A list of <typeparamref name="TEnum"/>.</returns>
442442
public static List<TEnum> ToEnumList<TEnum>(this StringValues? values, params char[] separators) where TEnum : struct, Enum {
443443

444-
List<TEnum> list = new();
444+
List<TEnum> list = [];
445445
if (values is null) return list;
446446

447447
foreach (string? value in values) {

0 commit comments

Comments
 (0)