Skip to content

Blazor access modifiers and field underscores #18446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Components/Samples/BlazorServerApp/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<h1>Counter</h1>

<p>Current count: @currentCount</p>
<p>Current count: @_currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
int currentCount = 0;
private int _currentCount = 0;

void IncrementCount()
private void IncrementCount()
{
currentCount++;
_currentCount++;
}
}
8 changes: 4 additions & 4 deletions src/Components/Samples/BlazorServerApp/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
</div>

@code {
bool collapseNavMenu = true;
private bool _collapseNavMenu = true;

string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private string NavMenuCssClass => _collapseNavMenu ? "collapse" : null;

void ToggleNavMenu()
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
_collapseNavMenu = !_collapseNavMenu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null)
@if (_forecasts == null)
{
<p><em>Loading...</em></p>
}
Expand All @@ -24,7 +24,7 @@ else
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
@foreach (var forecast in _forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
Expand All @@ -38,14 +38,14 @@ else
}

@code {
private WeatherForecast[] forecasts;
private WeatherForecast[] _forecasts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ones above I'm 😐 about since they're sample. However, going by my discussion, we would use this. to disambiguate locals versus global, and not prefix items with underscores.


protected override async Task OnInitializedAsync()
{
@*#if (Hosted)
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("WeatherForecast");
_forecasts = await Http.GetJsonAsync<WeatherForecast[]>("WeatherForecast");
#else
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
_forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
#endif*@
}

Expand All @@ -54,10 +54,10 @@ else
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public string Summary { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
#endif*@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@ namespace BlazorWasm_CSharp.Server.Controllers
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
private static readonly string[] _summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> logger;
private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
this.logger = logger;
this._logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();

return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
Summary = _summaries[rng.Next(_summaries.Length)]
})
.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public string Summary { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public class WeatherForecast
{
public DateTime Date { get; set; }

public string Summary { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string Summary { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ namespace BlazorServerWeb_CSharp.Data
{
public class WeatherForecastService
{
private static readonly string[] Summaries = new[]
private static readonly string[] _summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
var rng = new Random();

return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
Summary = _summaries[rng.Next(_summaries.Length)]
}).ToArray());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<h1>Counter</h1>

<p>Current count: @currentCount</p>
<p>Current count: @_currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;
private int _currentCount = 0;

private void IncrementCount()
{
currentCount++;
_currentCount++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<p>This component demonstrates fetching data from a service.</p>

@if (forecasts == null)
@if (_forecasts == null)
{
<p><em>Loading...</em></p>
}
Expand All @@ -23,7 +23,7 @@ else
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
@foreach (var forecast in _forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
Expand All @@ -37,10 +37,10 @@ else
}

@code {
private WeatherForecast[] forecasts;
private WeatherForecast[] _forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
_forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<AuthorizeView>
<Authorized>
@if (canEditProfile)
@if (_canEditProfile)
{
<a href="AzureADB2C/Account/EditProfile">Hello, @context.User.Identity.Name!</a>
}
Expand All @@ -20,11 +20,11 @@
</AuthorizeView>

@code {
private bool canEditProfile;
private bool _canEditProfile;

protected override void OnInitialized()
{
var options = AzureADB2COptions.Get(AzureADB2CDefaults.AuthenticationScheme);
canEditProfile = !string.IsNullOrEmpty(options.EditProfilePolicyId);
_canEditProfile = !string.IsNullOrEmpty(options.EditProfilePolicyId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
</div>

@code {
private bool collapseNavMenu = true;

private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private bool _collapseNavMenu = true;

private string NavMenuCssClass => _collapseNavMenu ? "collapse" : null;

private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
_collapseNavMenu = !_collapseNavMenu;
}
}