ReSharper 2024.1 Help

Code Inspection: Replace with single assignment

Category

Common Practices and Code Improvements

ID

ReplaceWithSingleAssignment.True

EditorConfig

resharper_replace_with_single_assignment_true_highlighting

Default severity

Suggestion

Language

C#

Requires SWA

No

To set a value for a bool variable depending on a certain condition, you can use a single statement in which you join the condition checking and the assignment.

Below, the result of the Contains method determines whether bool a will be true or false. Contains returns bool, so you can assign the result of the method directly to a. ReSharper helps you eliminate the If statement and assign the result of Contains to a.

private static void TestConvertIf(string s) { bool a = true; if (s.Contains(".")) { a = false; } Console.WriteLine(a); }
private static void TestConvertIf(string s) { bool a = !s.Contains("."); Console.WriteLine(a); }
Last modified: 15 April 2024