Currently I have the following ternary operation:
string value = "";
value = Company == null ? Name : Name + "," + Company;
Everything works great. However, I'd like to check if Company is null or "".
Is there a way to do that using a ternary statement?
use string.IsNullOrEmpty
to check for null and empty string.
value = string.IsNullOrEmpty(Company) ? Name : Name + "," + Company;
if you are using .Net framework 4.0 or higher, and you want to consider white space as empty string as well then you can use string.IsNullOrWhiteSpace
Use this String.IsNullOrWhiteSpace
value = string.IsNullOrWhiteSpace(Company) ? Name : Name + "," + Company;
of course you can also use this:
value = (Company == null || Company == "") ? Name : Name + "," + Company;
it's just for clarification that you can use more comples statements
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With