Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:40:57+00:00 2026-06-13T22:40:57+00:00

In following code (Josh Smith’s article on MVVM), can somebody give me some insight

  • 0

In following code (Josh Smith’s article on MVVM), can somebody give me some insight about
return _canExecute == null ? true : _canExecute(parameter); ?

it is a normal if/else statement but I’m not getting the last part of it.

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;        

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
    : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;           
    }
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:40:58+00:00Added an answer on June 13, 2026 at 10:40 pm

    _canExecute is lambda function, and it can be null, depending on which constructor will instantiate RelayCommand object. Thus, the implementation of CanExecute method checks whether this function is set, and if it’s not, it returns true, wheras if there is function assigned, it evaluates it (given the parameter), and the evaluated value is returned as a result for CanExecute.

    In a nutshell: CanExecute will be evaluated using whatever predicate is passed along in the constructor, and in case of lack one – will always return true.

    You’ve asked that’s usually used for array/lists – it’s very similar situation. The predicate is just a function that you can pass around. When you pass such predicate to some method that is filtering a collection, the method is just calling this predicate as it would any other function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While copying code for RelayCommand from Josh Smith article I copied following code public
The following code is from the MVVM sample by Josh Smith: /// <summary> ///
I'm writing some WPF code involving Adorners. I'm using Josh Smith's UIElementAdorner.cs (found in
why following code throwing ConcurrentModificationException? Josh Bloch can avoid ConcurrentModificationException. ArrayList<Integer> list=new ArrayList<Integer>(); list.add(100);
Following code return error Can't convert String onto integer, please help subject = ['eng','Math','Sci']
I hope someone can help me... Josh Smith did a great article on WPF
Following code iterates through many data-rows, calcs some score per row and then sorts
Following code i am writing to hide some buttons in viewDidLoad. Here Buttons Are
Following code is not compiling, can anybody please help what is wrong here class
Following code is an example, I just want to know if this can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.