Skip to content

CA2208 - Undocumented check: ArgumentNullException paramName argument should match an actual method parameterΒ #49421

@jacob-finn-hs1

Description

@jacob-finn-hs1

Type of issue

Missing information

Description

The following compiler warning is not reflected in the documentation.

warning CA2208: Method Foo passes 'bar' as the paramName argument to a ArgumentNullException constructor. Replace this argument with one of the method's parameter names. Note that the provided parameter name should have the exact casing as declared on the method. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2208)

Example:

public void DoFoo(Foo foo) {
    Bar? bar = foo.Bar;
    if (bar is null) throw new ArgumentNullException(nameof(bar), $"Foo named {foo.Name} had no Bar!"); // CA2208
}

The documentation says a lot about not mixing up the parameter name with the message, but doesn't say anything about the parameter name needing to match a method parameter.

Attempting to use NullReferenceException results in CA2201. I'm left wondering what to do if I want to raise an exception when some non-parameter variable is null.

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2208

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca2208.md

Document Version Independent Id

cc2b5015-89a9-02ff-a585-fca3449dfa9a

Platform Id

09de10c1-92b4-8a2d-2ff4-db0e71226d62

Article author

@gewarren

Metadata

  • ID: ff82be90-2d17-5880-4edd-a89b18a3ae2b
  • PlatformId: 09de10c1-92b4-8a2d-2ff4-db0e71226d62
  • Service: dotnet-fundamentals

Related Issues


Associated WorkItem - 502087

Metadata

Metadata

Labels

πŸ“Œ seQUESTeredIdentifies that an issue has been imported into Quest.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions