C#

C#

Made by DeepSource

Use string.IsNullOrEmpty or string.IsNullOrWhiteSpace to check for empty strings CS-R1014

Anti-pattern
Major
Autofix

Comparing a string against an empty string literal is valid and is the preferred way in languages such as Go and Python. In C# however, it is recommended that you use the convenience methods such as string.IsNullOrWhiteSpace or string.IsNullOrEmpty as they offer slightly better performance when compared to other traditional/naive implementations.

Bad Practice

if (str == "")
{
    Console.WriteLine("`str` is empty");
}

Recommended

if (string.IsNullOrWhiteSpace(str))
{
    Console.WriteLine("str is empty");
}

References