|
| 1 | +--- |
| 2 | +title: "CA1877: Use 'Path.Combine' or 'Path.Join' overloads (code analysis)" |
| 3 | +description: "Learn about code analysis rule CA1877: Use 'Path.Combine' or 'Path.Join' overloads" |
| 4 | +ms.date: 11/05/2025 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | + - CA1877 |
| 8 | + - UsePathCombineOrPathJoinAnalyzer |
| 9 | +helpviewer_keywords: |
| 10 | + - CA1877 |
| 11 | +dev_langs: |
| 12 | + - CSharp |
| 13 | + - VB |
| 14 | +ai-usage: ai-generated |
| 15 | +--- |
| 16 | + |
| 17 | +# CA1877: Use 'Path.Combine' or 'Path.Join' overloads |
| 18 | + |
| 19 | +| Property | Value | |
| 20 | +|-------------------------------------|----------------------------------------| |
| 21 | +| **Rule ID** | CA1877 | |
| 22 | +| **Title** | Use `Path.Combine` or `Path.Join` overloads | |
| 23 | +| **Category** | [Performance](performance-warnings.md) | |
| 24 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 25 | +| **Enabled by default in .NET 10** | As suggestion | |
| 26 | + |
| 27 | +## Cause |
| 28 | + |
| 29 | +Multiple consecutive <xref:System.IO.Path.Combine%2A?displayProperty=nameWithType> or <xref:System.IO.Path.Join%2A?displayProperty=nameWithType> operations are used to build a path. |
| 30 | + |
| 31 | +## Rule description |
| 32 | + |
| 33 | +When you use multiple consecutive `Path.Combine` or `Path.Join` operations, it's more efficient to use an overload that accepts multiple path segments. This approach reduces the number of allocations and function calls, improving performance. Both methods provide overloads that accept multiple parameters, allowing you to collapse consecutive operations into a single call. |
| 34 | + |
| 35 | +## How to fix violations |
| 36 | + |
| 37 | +Replace consecutive `Path.Combine` or `Path.Join` operations with a single call using an overload that accepts all path segments. |
| 38 | + |
| 39 | +A *code fix* that automatically performs this transformation is available. |
| 40 | + |
| 41 | +## Example |
| 42 | + |
| 43 | +The following code snippet shows a violation of CA1877: |
| 44 | + |
| 45 | +:::code language="csharp" source="./snippets/csharp/all-rules/CA1877.cs" id="Violation"::: |
| 46 | +:::code language="vb" source="./snippets/vb/all-rules/CA1877.vb" id="Violation"::: |
| 47 | + |
| 48 | +The following code snippet fixes the violation: |
| 49 | + |
| 50 | +:::code language="csharp" source="./snippets/csharp/all-rules/CA1877.cs" id="Fix"::: |
| 51 | +:::code language="vb" source="./snippets/vb/all-rules/CA1877.vb" id="Fix"::: |
| 52 | + |
| 53 | +## When to suppress warnings |
| 54 | + |
| 55 | +It's safe to suppress a warning from this rule if performance isn't a concern. |
| 56 | + |
| 57 | +## Suppress a warning |
| 58 | + |
| 59 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 60 | + |
| 61 | +```csharp |
| 62 | +#pragma warning disable CA1877 |
| 63 | +// The code that's violating the rule is on this line. |
| 64 | +#pragma warning restore CA1877 |
| 65 | +``` |
| 66 | + |
| 67 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 68 | + |
| 69 | +```ini |
| 70 | +[*.{cs,vb}] |
| 71 | +dotnet_diagnostic.CA1877.severity = none |
| 72 | +``` |
| 73 | + |
| 74 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
| 75 | + |
| 76 | +## See also |
| 77 | + |
| 78 | +- [Performance rules](performance-warnings.md) |
| 79 | +- <xref:System.IO.Path.Combine%2A?displayProperty=nameWithType> |
| 80 | +- <xref:System.IO.Path.Join%2A?displayProperty=nameWithType> |
0 commit comments