If you really want to do this in terms of optimization, string.IsNullOrWhiteSpace(myString) will have better performance, as it will be able to immediately return the result.
Take the following line:
" BC " (4 trailing spaces)
With string.IsNullOrEmpty(myString.Trim()) :
- Trim string, iterate over 5 characters (1 previous and 4 trailing spaces), resulting in "BC"
- IsNullOrEmpty iterates 1 character and returns false.
A total of 6 characters are marked.
With string.IsNullOrWhiteSpace(myString) :
- Iterate over two characters, returns false for the second character
Only 2 characters checked.
The greater the number of string.IsNullOrWhiteSpace(myString) spaces, the greater the benefits of string.IsNullOrWhiteSpace(myString) will be provided over the alternative.
As states in other answers and comments, creating an additional row result from Trim() adds extra overhead.
source share