Do you use String.Empty instead of ""?

Updated by Brook Jeynes [SSW] 2 years ago. See history

123
<introEmbed body={<> Since .NET 5+, the choice between using String.Empty and "" is a stylistic decision for the team. In .NET Framework, "" is less efficient than String.Empty from a memory perspective which can result in better performance due to faster garbage collection. From the team that worked on performance in .NET: [String.Empty vs "" in modern .NET language](https://youtube.com/clip/UgkxIv8HnqTApTo6VOuEvBkAx3VnjY6RioCh) </>} />
public string myString
{
get
{
return ;
}
}

❌ Figure: Figure: Bad code if used in .NET Framework. Low performance

public string myString
{
get
{
return string.Empty;
}
}

✅ Figure: Figure: Good code if used in .NET Framework. Higher performance

We have a program called SSW Code Auditor to check for this rule.

Acknowledgements

Adam Cogan
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star