code4it-dev / blog-comments

https://www.code4it.dev/
1 stars 0 forks source link

blog/csharp-check-if-string-is-empty #13

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

How to check if a string is really empty with C# - Code4IT

Is a string empty? What if it contains only white spaces? You shouldn't reinvent the wheel, since .NET exposes methods exactly for these cases: String.IsNullOrEmpty and String.IsNullOrWhiteSpace.

https://www.code4it.dev/blog/csharp-check-if-string-is-empty

georgemcz commented 3 years ago

Also, be careful as your method IsStringEmpty allocates new string each time the Trim is called (basically), this might be problematic in a loop or something. Just one more argument why not to reinvent the wheel 😀

bellons91 commented 3 years ago

Yes indeed! Also, if you forget to check for null values before calling the Trim method, you'll end up with a NullReferenceException.