# Initializing commands C# [closed]

    private readonly ICommand onSelectIssueCommand = null;

public ICommand OnSelectedIssueCommand
{
get { return onSelectIssueCommand ?? new Command<object>(OnEventSelected); }
}


I'm trying to find out the best way to initialize commands, I really like this way but someone recently told me this was wrong. I haven't had any problems with it whatsoever but I would like to learn the best way to do it. Mainly use commands in xamarin-mobile development context

• You need to provide more context. – t3chb0t Mar 16 '18 at 15:17
• What more would you like to know? I'm just wondering if this is an acceptable way to initalize my commands? – JsonDork Mar 16 '18 at 15:19
• But this isn't initializing anything. onSelectIssueCommand always remains null. – t3chb0t Mar 16 '18 at 15:21
• Really? But then it shouldn't be working at all. Maybe it gets initalized during the xamarin initalizecomponent stage? Seems like that always enters it. – JsonDork Mar 16 '18 at 15:25

Isn't this way always creating a new Command when the getter is called ?

I would have done something like this :

private ICommand onSelectIssueCommand = null;

public ICommand OnSelectedIssueCommand
{
get
{
if (onSelectIssueCommand == null)
onSelectIssueCommand = new Command<object>(OnEventSelected);
return onSelectIssueCommand;
}
}

• Technically it is called every time the getter is called, but shoudln't the getter only be called once during initalization of pages? – JsonDork Mar 16 '18 at 15:21
• You may need to call it again if you need to raise the CanExecuteChanged event. – Mohammed Larabi Mar 16 '18 at 15:23
• Good Point! Didn't think about that – JsonDork Mar 16 '18 at 15:24