Use
string.IsNullOrWhiteSpace
instead of comparing against empty string 87
88 }
89
90 if (emptyString == "") 91 {
92 Console.WriteLine("String is empty!");
93 }
Description
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");
}