Skip to content

Commit

Permalink
Added documentation for new rule CA1846 - Use string.Contains(char) w…
Browse files Browse the repository at this point in the history
…henever possible
  • Loading branch information
MeikTranel committed May 18, 2021
1 parent 914740e commit 18cac92
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
74 changes: 74 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca1846.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: "CA1846: Use char literal for a single character lookup"
description: "Use string.Contains(char) instead of string.Contains(string) when searching for a single character"
ms.date: 03/03/2021
ms.topic: reference
f1_keywords:
- "CA1846"
helpviewer_keywords:
- "CA1846"
author: MeikTranel
dev_langs:
- CSharp
- VB
---

# CA1846: Use string.Contains(char) instead of string.Contains(string) with single characters

| | Value |
|-|-|
| **Rule ID** |CA1846|
| **Category** |[Performance](performance-warnings.md)|
| **Fix is breaking or non-breaking** |Non-breaking|

## Cause

`string.Contains(string)` is used when `string.Contains(char)` was available.

## Rule description

When searching for a single character, using `string.Contains(char)` offers better performance than `string.Contains(string)`.

## How to fix violations

In general, the rule is fixed simply by using a char literal instead of a string literal.

```csharp
public bool ContainsLetterI()
{
var testString = "I am a test string.";
return testString.Contains("I");
}
```

```vb
Public Function ContainsLetterI as Boolean
Dim testString As String = "I am a test string."
Return testString.Contains("I")
End Function
```

This code can be changed to use a char literal instead.

```csharp
public bool ContainsLetterI()
{
var testString = "I am a test string.";
return testString.Contains('I');
}
```

```vb
Public Function ContainsLetterI as Boolean
Dim testString As String = "I am a test string."
Return testString.Contains("I"c)
End Function
```

## When to suppress warnings

Suppress a violation of this rule if you're not concerned about the performance impact of the search invocation in question.

## See also

- [Performance rules](performance-warnings.md)
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ The following table lists code quality analysis rules.
> |[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. |
> | [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. |
> | [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. |
> | [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. |
> | [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. |
> |[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. |
> | [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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ Performance rules support high-performance libraries and applications.
| [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. |
| [CA1837: Use `Environment.ProcessId` instead of `Process.GetCurrentProcess().Id`](ca1837.md) | `Environment.ProcessId` is simpler and faster than `Process.GetCurrentProcess().Id`. |
| [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. |
| [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. |
2 changes: 2 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ items:
href: code-analysis/quality-rules/ca1837.md
- name: CA1838
href: code-analysis/quality-rules/ca1838.md
- name: CA1846
href: code-analysis/quality-rules/ca1846.md
- name: Publish rules
items:
- name: Overview
Expand Down

0 comments on commit 18cac92

Please sign in to comment.