RequireNamedArgs 0.0.7

dotnet add package RequireNamedArgs --version 0.0.7                
NuGet\Install-Package RequireNamedArgs -Version 0.0.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RequireNamedArgs" Version="0.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RequireNamedArgs --version 0.0.7                
#r "nuget: RequireNamedArgs, 0.0.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RequireNamedArgs as a Cake Addin
#addin nuget:?package=RequireNamedArgs&version=0.0.7

// Install RequireNamedArgs as a Cake Tool
#tool nuget:?package=RequireNamedArgs&version=0.0.7                

Require a method to be invoked with named arguments

This package provides a Roslyn code analyzer and an accompanying code-fix provider, that let you force named arguments in C#.

How to use it?

Install this nuget package.

Introduce RequireNamedArgsAttribute attribute to your solution. In other words, place the following C# code in an appropriate spot in your solution.

[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct)]
class RequireNamedArgsAttribute : Attribute {}

Apply the [RequireNamedArgs] attribute to the methods that should only be called with named arguments. For example:

[RequireNamedArgs]
public static void TellPowerLevel(string name, int powerLevel) {}

// Elsewhere in your code:
// if `TellPowerLevel` method is called with positional arguments,
// the analyzer will emit an error.
TellPowerLevel(name: "Goku", powerLevel: 9001);

Supported method kinds

The analyzer supports requiring named arguments for the following method kinds

  • Regular instance and static methods
  • Extension methods
  • Regular constructors
  • Attribute constructors
  • Primary constructors

To mark a record's primary constructor, apply [RequireNamedArgs] to the record itself.

[RequireNamedArgs]
record Character(string Name, int PowerLevel) {}

[RequireNamedArgs]
record struct CharacterStruct(string Name, int PowerLevel) {}

// Elsewhere in your code:
// if the primary constructor of `Character` or `CharacterStruct` is called with positional arguments,
// the analyzer will emit an error.
new Character(Name: "Goku", PowerLevel: 9001);
new CharacterStruct(Name: "Goku", PowerLevel: 9001);

Configuration

Starting in Visual Studio 2019 version 16.3, you can configure the severity of analyzer rules, or diagnostics, in an EditorConfig file, from the light bulb menu, and the error list.

You can add the following to the [*.cs] section of your .editorconfig.

[*.cs]
dotnet_diagnostic.RequireNamedArgs.severity = error

The possible severity values are:

  • error
  • warning
  • suggestion
  • silent
  • none
  • default (in case of this analyzer, it's equal to error)

Please take a look at the documentation for a detailed description.

Thank you!

License

The RequireNamedArgs analyzer and code-fix provider are licensed under the MIT license.
So they can be used freely in commercial applications.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.7 42,983 11/9/2023
0.0.6 1,451 2/19/2023
0.0.5 17,886 4/18/2021
0.0.4 335 4/17/2021
0.0.3 868 8/23/2020
0.0.2 514 12/22/2019
0.0.1 865 8/16/2018

- Added support for C# primary constructors
           - Misc minor improvements and clean-ups