可以說我有
class foo
{
public List<string> Values {get;set;}=New()
}
并在組件中
foo? bar;
protected override async Task OnInitializedAsync()
{
bar= await _ds.Get<foo>();
}
所以現在在剃須刀中還有其他/更好的方法來進行 nullcheck 嗎?
@if (bar!= null)@foreach(var x in bar.Values)
{
<MudSelectItem Value="x">@x</MudSelectItem>
}
最好是這樣的
@foreachWhenNotnull(var x in bar.Values)...
我知道我可以像一個組件那樣做,但也許這是最簡單的方法?
謝謝并恭祝安康
uj5u.com熱心網友回復:
最好檢查它是否null像您自己的代碼中那樣,因為根據它是否為空,您可以決定例如顯示錯誤或訊息。但是,如果您只是想避免出現System.NullReferenceException 錯誤,則可以執行以下操作
使用 空條件運算子
@foreach (var x in bar?.Values ?? new())
{
<MudSelectItem Value="x">@x</MudSelectItem>
}
你也可以這樣做:
foo bar;
protected override async Task OnInitializedAsync()
{
bar = (await _ds.Get<foo>()) ?? new();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/518521.html
標籤:。网西装外套
上一篇:[FromForm]中串列的復雜系結不起作用.net6
下一篇:火花提交到遠程火花
