An array is a fixed-size, ordered data structure whose elements share a declared type and are accessed by numeric index. C# expresses an array of Product values as Product[]. section 5.1.3
Arrays can contain nullable elements when their element type includes ?. Product?[] can hold Product objects or null, while Product[]? allows only Product elements but permits the array reference itself to be null. section 5.4.3
ASP.NET Core uses arrays in ordinary application code and during request processing:
- A controller can pass a string array to a Razor view, where foreach renders each value. section 5.1.3
- The default model binder creates an array from multiple form fields sharing the same name. section 28.5.1
- Named indexes such as Data[1] and Data[0] override the default order used to populate the bound array. section 28.5.1
Products with a nullable element
Product?[] GetProducts() returns kayak, lifejacket, and null. Code that displays the result filters out null values before rendering each product. section 5.4.3
Listing 28.22 The contents of the Bindings.cshtml file in the Pages folder
@page "/pages/bindings"
@model BindingsModel
@using Microsoft.AspNetCore.Mvc
@using Microsoft.AspNetCore.Mvc.RazorPages
<div class="container-fluid">
<div class="row">
<div class="col">
<form asp-page="Bindings" method="post">
<div class="form-group">
<label>Value #1</label>
<input class="form-control" name="Data"
value="Item 1" />
</div>
<div class="form-group">
<label>Value #2</label>
<input class="form-control" name="Data"
value="Item 2" />
</div>
<div class="form-group">
<label>Value #3</label>
<input class="form-control" name="Data"
value="Item 3" />
</div>
<button type="submit" class="btn btn-primary">
Submit
</button>
<a class="btn btn-secondary" asp-page="Bindings">Reset</a>
</form>
</div>
<div class="col">
<ul class="list-group">
@foreach (string s in Model.Data.Where(s => s != null)) {
<li class="list-group-item">@s</li>
}
</ul>
</div>
</div>
</div>
@functions {
public class BindingsModel : PageModel {
[BindProperty(Name = "Data")]
public string[] Data { get; set; } = Array.Empty<string>();
}
}
Inspect the Razor Page form that submits three values under the shared Data name and supplies them to array model binding. section 28.5.1







































