SharpLab.のnull結合演算子の記事を見て、すっかりnull結合演算子のことを忘れてる自分がいることに気づきました。
SharpLab.さんの所に、例であがってるM-V-VMのViewModelでのCommandの定義で、null結合演算子を使わないパターンのコードがあがってますが、自分も例外なくこれで書いてましたorz
private ICommand _sampleCommand;
public ICommand SampleCommand
{
get
{
if (_sampleCommand == null)
{
_sampleCommand = new DelegateCommand(SampleAction);
}
return _sampleCommand;
}
}
こんなにすっきりになるなんて…!
private ICommand _sampleCommand;
public ICommand SampleCommand
{
get
{
return _sampleCommand = _sampleCommand ??
new DelegateCommand(SampleAction);
}
}
次からは、こう書こうと心に決めてメモメモ。(自分の所に書いておかないと忘れる)