Skip to content

Commit 18cac92

Browse files
committed
Added documentation for new rule CA1846 - Use string.Contains(char) whenever possible
1 parent 914740e commit 18cac92

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: "CA1846: Use char literal for a single character lookup"
3+
description: "Use string.Contains(char) instead of string.Contains(string) when searching for a single character"
4+
ms.date: 03/03/2021
5+
ms.topic: reference
6+
f1_keywords:
7+
- "CA1846"
8+
helpviewer_keywords:
9+
- "CA1846"
10+
author: MeikTranel
11+
dev_langs:
12+
- CSharp
13+
- VB
14+
---
15+
16+
# CA1846: Use string.Contains(char) instead of string.Contains(string) with single characters
17+
18+
| | Value |
19+
|-|-|
20+
| **Rule ID** |CA1846|
21+
| **Category** |[Performance](performance-warnings.md)|
22+
| **Fix is breaking or non-breaking** |Non-breaking|
23+
24+
## Cause
25+
26+
`string.Contains(string)` is used when `string.Contains(char)` was available.
27+
28+
## Rule description
29+
30+
When searching for a single character, using `string.Contains(char)` offers better performance than `string.Contains(string)`.
31+
32+
## How to fix violations
33+
34+
In general, the rule is fixed simply by using a char literal instead of a string literal.
35+
36+
```csharp
37+
public bool ContainsLetterI()
38+
{
39+
var testString = "I am a test string.";
40+
return testString.Contains("I");
41+
}
42+
```
43+
44+
```vb
45+
Public Function ContainsLetterI as Boolean
46+
Dim testString As String = "I am a test string."
47+
Return testString.Contains("I")
48+
End Function
49+
```
50+
51+
This code can be changed to use a char literal instead.
52+
53+
```csharp
54+
public bool ContainsLetterI()
55+
{
56+
var testString = "I am a test string.";
57+
return testString.Contains('I');
58+
}
59+
```
60+
61+
```vb
62+
Public Function ContainsLetterI as Boolean
63+
Dim testString As String = "I am a test string."
64+
Return testString.Contains("I"c)
65+
End Function
66+
```
67+
68+
## When to suppress warnings
69+
70+
Suppress a violation of this rule if you're not concerned about the performance impact of the search invocation in question.
71+
72+
## See also
73+
74+
- [Performance rules](performance-warnings.md)

docs/fundamentals/code-analysis/quality-rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ The following table lists code quality analysis rules.
129129
> |[CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, <xref:System.Linq.Enumerable.Count%60%601%28System.Collections.Generic.IEnumerable%7B%60%600%7D%29> or <xref:System.Linq.Enumerable.LongCount%60%601%28System.Collections.Generic.IEnumerable%7B%60%600%7D%29> to determine whether the object contains or not any items. |
130130
> | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. |
131131
> | [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of 'StringBuilder' always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. |
132+
> | [CA1846: Use char literal for a single character lookup](ca1846.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. |
132133
> | [CA2000: Dispose objects before losing scope](ca2000.md) | Because an exceptional event might occur that will prevent the finalizer of an object from running, the object should be explicitly disposed before all references to it are out of scope. |
133134
> |[CA2002: Do not lock on objects with weak identity](ca2002.md) |An object is said to have a weak identity when it can be directly accessed across application domain boundaries. A thread that tries to acquire a lock on an object that has a weak identity can be blocked by a second thread in a different application domain that has a lock on the same object. |
134135
> | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a <xref:System.Threading.Tasks.Task> directly. When an asynchronous method awaits a <xref:System.Threading.Tasks.Task> directly, continuation occurs in the same thread that created the task. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. Consider calling <xref:System.Threading.Tasks.Task.ConfigureAwait(System.Boolean)?displayProperty=nameWithType> to signal your intention for continuation. |

docs/fundamentals/code-analysis/quality-rules/performance-warnings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ Performance rules support high-performance libraries and applications.
4949
| [CA1836: Prefer `IsEmpty` over `Count` when available](ca1836.md) | Prefer `IsEmpty` property that is more efficient than `Count`, `Length`, <xref:System.Linq.Enumerable.Count%60%601%28System.Collections.Generic.IEnumerable%7B%60%600%7D%29> or <xref:System.Linq.Enumerable.LongCount%60%601%28System.Collections.Generic.IEnumerable%7B%60%600%7D%29> to determine whether the object contains or not any items. |
5050
| [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. |
5151
| [CA1838: Avoid `StringBuilder` parameters for P/Invokes](ca1838.md) | Marshaling of `StringBuilder` always creates a native buffer copy, resulting in multiple allocations for one marshaling operation. |
52+
| [CA1846: Use char literal for a single character lookup](ca1846.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. |

docs/fundamentals/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ items:
766766
href: code-analysis/quality-rules/ca1837.md
767767
- name: CA1838
768768
href: code-analysis/quality-rules/ca1838.md
769+
- name: CA1846
770+
href: code-analysis/quality-rules/ca1846.md
769771
- name: Publish rules
770772
items:
771773
- name: Overview

0 commit comments

Comments
 (0)