[API Proposal]: ICommand.Null

Open
#107,435 11 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
api

Research direction

Start by reviewing the proposed System.Windows.Input.ICommand API and the existing Stream.Null and TextWriter.Null patterns. Check how ICommand is provided by System.ObjectModel.dll and whether the proposed static member and nested implementation fit the project’s API review requirements; done means an approved design with its implementation and validation plan.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.ComponentModel
Background and motivation

For abstractions like ICommand it's useful to have a standard implementation that does nothing. We have some of them already, such as Stream.Null and TextWriter.Null.

API Proposal
namespace System.Windows.Input;

public partial interface ICommand
{
    public static ICommand Null { get; } = new NullCommand();

    private sealed class NullCommand : ICommand
    {
        public event EventHandler? CanExecuteChanged
        {
            add {}
            remove {}
        }
    
        public bool CanExecute(object? parameter)
        {
            return true;
        }
    
        public void Execute(object? parameter)
        {
            // Do nothing
        }
    }
}
API Usage
private ICommand _command = ICommand.Null; // Might be set to something meaningful later
Alternative Designs

We could put it on a class instead, but we don't have a built-in implementation of ICommand in System.ObjectModel.dll, so I don't believe there is a good candidate. We could, of course, create one.

Risks

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.