File tree Expand file tree Collapse file tree 5 files changed +73
-1
lines changed
docs/fundamentals/code-analysis/quality-rules Expand file tree Collapse file tree 5 files changed +73
-1
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
title : " CA2017: Parameter count mismatch (code analysis)"
3
3
description : " Learn about code analysis rule CA2017: Parameter count mismatch"
4
- ms.date : 01/19/2022
4
+ ms.date : 09/24/2025
5
+ ai-usage : ai-assisted
5
6
f1_keywords :
6
7
- " LoggerMessageDefineAnalyzer"
7
8
- " CA2017"
8
9
helpviewer_keywords :
9
10
- " LoggerMessageDefineAnalyzer"
10
11
- " CA2017"
11
12
author : Youssef1313
13
+ dev_langs :
14
+ - CSharp
15
+ - VB
12
16
---
13
17
# CA2017: Parameter count mismatch
14
18
@@ -36,6 +40,14 @@ Match the number of placeholders in the template format with the number of passe
36
40
37
41
Do not suppress a warning from this rule.
38
42
43
+ ## Example
44
+
45
+ The following example shows methods that violate CA2017 and methods that satisfy the rule.
46
+
47
+ :::code language="csharp" source="snippets/csharp/all-rules/ca2017.cs" id="snippet1":::
48
+
49
+ :::code language="vb" source="snippets/vb/all-rules/ca2017-parameter-count-mismatch_1.vb":::
50
+
39
51
## See also
40
52
41
53
- [ Reliability rules] ( reliability-warnings.md )
Original file line number Diff line number Diff line change 9
9
10
10
<ItemGroup >
11
11
<PackageReference Include =" System.Data.SqlClient" Version =" 4.9.0" />
12
+ <PackageReference Include =" Microsoft.Extensions.Logging" Version =" 9.0.0" />
13
+ <PackageReference Include =" System.Data.OleDb" Version =" 10.0.0-rc.1.25451.107" />
12
14
<PackageReference Include =" System.Security.Permissions" Version =" 9.0.0" />
13
15
</ItemGroup >
14
16
Original file line number Diff line number Diff line change
1
+ using Microsoft . Extensions . Logging ;
2
+
3
+ namespace ca2017
4
+ {
5
+ // <snippet1>
6
+ public class LoggingExample
7
+ {
8
+ private readonly ILogger < LoggingExample > _logger ;
9
+
10
+ public LoggingExample ( ILogger < LoggingExample > logger )
11
+ {
12
+ _logger = logger ;
13
+ }
14
+
15
+ public void ExampleMethod ( )
16
+ {
17
+ string name = "Alice" ;
18
+ int age = 30 ;
19
+
20
+ // Violates CA2017: Too few arguments for placeholders.
21
+ _logger . LogInformation ( "User {Name} is {Age} years old and lives in {City}" , name , age ) ;
22
+
23
+ // Violates CA2017: Too many arguments for placeholders.
24
+ _logger . LogError ( "Error occurred: {Message}" , "Something went wrong" , "Extra argument" ) ;
25
+
26
+ // Correct usage: Matching number of placeholders and arguments.
27
+ _logger . LogInformation ( "User {Name} is {Age} years old" , name , age ) ;
28
+ }
29
+ }
30
+ // </snippet1>
31
+ }
Original file line number Diff line number Diff line change 8
8
</PropertyGroup >
9
9
10
10
<ItemGroup >
11
+ <PackageReference Include =" Microsoft.Extensions.Logging" Version =" 9.0.0" />
12
+ <PackageReference Include =" System.Data.OleDb" Version =" 10.0.0-rc.1.25451.107" />
11
13
<PackageReference Include =" System.Data.SqlClient" Version =" 4.9.0" />
12
14
<PackageReference Include =" System.Security.Permissions" Version =" 9.0.0" />
13
15
</ItemGroup >
Original file line number Diff line number Diff line change
1
+ Imports Microsoft.Extensions.Logging
2
+
3
+ Public Class LoggingExample
4
+
5
+ Private ReadOnly _logger As ILogger( Of LoggingExample)
6
+
7
+ Public Sub New (logger As ILogger( Of LoggingExample))
8
+ _logger = logger
9
+ End Sub
10
+
11
+ Public Sub ExampleMethod()
12
+ Dim name As String = "Alice"
13
+ Dim age As Integer = 30
14
+
15
+ ' Violates CA2017: Too few arguments for placeholders.
16
+ _logger.LogInformation( "User {Name} is {Age} years old and lives in {City}" , name, age)
17
+
18
+ ' Violates CA2017: Too many arguments for placeholders.
19
+ _logger.LogError( "Error occurred: {Message}" , "Something went wrong" , "Extra argument" )
20
+
21
+ ' Correct usage: Matching number of placeholders and arguments.
22
+ _logger.LogInformation( "User {Name} is {Age} years old" , name, age)
23
+ End Sub
24
+
25
+ End Class
You can’t perform that action at this time.
0 commit comments