string.IsNullOrEmpty
or string.IsNullOrWhiteSpace
to check for empty strings CS-R1014Comparing 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.
if (str == "")
{
Console.WriteLine("`str` is empty");
}
if (string.IsNullOrWhiteSpace(str))
{
Console.WriteLine("str is empty");
}