CommunityToolkit / CommunityToolkit/dotnet

`[RelayCommand]` does not work with default/optional arguments as the command parameter

Open
#1,088 0 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

The following command compiles, but causes a runtime error when called.

```csharp
public override async Task SomeCommandAsync(bool optional = false, CancellationToken cancellationToken = default)
{

}
```

The same command without the first optional argument does work, i.e. the optional `CancellationToken` is handled correctly, as far as I can tell.

If it isn't possible to dispatch the command with that default/optional argument, then the analyzer should probably produce a compiler error notifying the user that `RelayCommand`s cannot have optional arguments.

### Regression

_No response_

### Steps to reproduce

1. Paste the code above into a class inside a project that references the CommunityToolkit
2. Run the application

### Expected behavior

Either pass in the provided value or the default or generate a compiler error to notify the user that commands cannot have optional arguments.

### Screenshots

_No response_

### IDE and version

VS 2022

### IDE version

_No response_

### Nuget packages

- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [x] CommunityToolkit.Mvvm (aka MVVM Toolkit)

### Nuget package version(s)

CommunityToolkit.Mvvm 8.4.0

### Additional context

_No response_

### Help us help you

No, just wanted to report this

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.