-
Couldn't load subscription status.
- Fork 6.1k
Description
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
Document Version Independent Id
cc2b5015-89a9-02ff-a585-fca3449dfa9a
Platform Id
09de10c1-92b4-8a2d-2ff4-db0e71226d62
Article author
Metadata
- ID: ff82be90-2d17-5880-4edd-a89b18a3ae2b
- PlatformId: 09de10c1-92b4-8a2d-2ff4-db0e71226d62
- Service: dotnet-fundamentals
Metadata
Metadata
Labels
Type
Projects
Status