Adopt C# 12 collection expressions for empty lists/dicts

Refactored code to use C# 12 collection expressions ([]) for initializing empty lists and dictionaries instead of the older constructors. This change modernizes and simplifies collection initialization across models, services, and API clients without altering any logic.
This commit is contained in:
OlgunR
2026-04-20 14:33:08 +02:00
parent fcbc66f8f5
commit 0c936d0bf9
6 changed files with 10 additions and 10 deletions

View File

@@ -162,7 +162,7 @@ public abstract class BandGridBase<TItem> : ComponentBase
protected void UpdateBandOptions()
{
bandOptions = new List<BandOption> { new() { Id = string.Empty, Caption = "Ohne Band" } };
bandOptions = [new() { Id = string.Empty, Caption = "Ohne Band" }];
bandOptions.AddRange(bandLayout.Bands.Select(b => new BandOption { Id = b.Id, Caption = b.Caption }));
}

View File

@@ -4,8 +4,8 @@ namespace DbFirst.BlazorWebApp.Models.Grid
{
public class BandLayout
{
public List<BandDefinition> Bands { get; set; } = new();
public List<string> ColumnOrder { get; set; } = new();
public List<BandDefinition> Bands { get; set; } = [];
public List<string> ColumnOrder { get; set; } = [];
public Dictionary<string, string?> ColumnWidths { get; set; } = new(StringComparer.OrdinalIgnoreCase);
public GridPersistentLayout? GridLayout { get; set; }
public SizeMode SizeMode { get; set; } = SizeMode.Medium;
@@ -15,7 +15,7 @@ namespace DbFirst.BlazorWebApp.Models.Grid
{
public string Id { get; set; } = string.Empty;
public string Caption { get; set; } = string.Empty;
public List<string> Columns { get; set; } = new();
public List<string> Columns { get; set; } = [];
}
public class BandOption

View File

@@ -88,9 +88,9 @@ namespace DbFirst.BlazorWebApp.Services
Dictionary<string, ColumnDefinition> columnLookup)
{
layout ??= new BandLayout();
layout.Bands ??= new List<BandDefinition>();
layout.ColumnOrder ??= new List<string>();
layout.ColumnWidths ??= new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
layout.Bands ??= [];
layout.ColumnOrder ??= [];
layout.ColumnWidths ??= [];
foreach (var band in layout.Bands)
{

View File

@@ -17,7 +17,7 @@ public class CatalogApiClient : ICatalogApiClient
public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken ct = default)
{
var result = await _httpClient.GetFromJsonAsync<List<CatalogReadDto>>(Endpoint, ct);
return result ?? new List<CatalogReadDto>();
return result ?? [];
}
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken ct = default)

View File

@@ -16,6 +16,6 @@ public class DashboardApiClient : IDashboardApiClient
public async Task<List<DashboardInfoDto>> GetAllAsync(CancellationToken ct = default)
{
var result = await _httpClient.GetFromJsonAsync<List<DashboardInfoDto>>(Endpoint, ct);
return result ?? new List<DashboardInfoDto>();
return result ?? [];
}
}

View File

@@ -33,7 +33,7 @@ public class MassDataApiClient : IMassDataApiClient
var url = query.Count == 0 ? Endpoint : $"{Endpoint}?{string.Join("&", query)}";
var result = await _httpClient.GetFromJsonAsync<List<MassDataReadDto>>(url, ct);
return result ?? new List<MassDataReadDto>();
return result ?? [];
}
public async Task<ApiResult<MassDataReadDto?>> UpsertAsync(MassDataWriteDto dto, CancellationToken ct = default)