liveBook · Manning

Array

As explained in Pro ASP.NET Core 7, Tenth Edition

An array is a fixed-size ordered data structure whose elements are accessed by numeric index and share a declared element type.

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

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains typed and nullable array declarations, then works through rendering, repeated form names, null filtering, and explicit indexes.
Examples
Demonstrates Product[], string arrays in views, nullable product elements, repeated Data inputs, null filtering, and indexed form names.
What it assumes
Assumes basic C# classes, properties, controllers, views, forms, and Razor syntax while demonstrating array nullability from concrete code.
What you can do after
Apply it via C# and ASP.NET Core by declaring typed arrays, handling nullable elements, rendering values, and binding repeated form inputs.
How it connects
Connects arrays with nullable types, collections, Razor views, Razor Pages, and model binding.

Also explained in

Compare how 40 books explain array →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.