Skip to content

Blazor nullable and null-state static analysis guidance #23392

@guardrex

Description

@guardrex

Starting with C# 8 (.NET Core 3.0) and defaulting project templates into a <Nullable> value of enabled with 6.0 or later, add a smidgen of nullable guidance to the end of the Component parameters 6.0 section. We want to instruct developers that either making the parameters nullable or assigning an initial value, which is valid, are the best practices.

For an example of making component parameters nullable, the updated templates do that today in the SurveyPrompt component for the Title parameter ...

@code {
    // Demonstrates how a parent component can supply parameters
    [Parameter]
    public string? Title { get; set; }
}

The compiler is also quite happy (no warning) assigning an initial value to ensure that the component parameter isn't null ...

public string Title { get; set; } = "Initial Title";

The dev will need to adjust code as needed to avoid nulls thereafter, the null operator and conditional checks being the most common approaches.

This issue also serves as a general sweep of 6.0 Blazor docs and snippet samples. The sweep might be performed separately.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions