Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ternary statement Null or Blank

Tags:

c#

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?

like image 819
webdad3 Avatar asked Nov 29 '22 01:11

webdad3


2 Answers

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

like image 73
Habib Avatar answered Dec 05 '22 03:12

Habib


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

like image 45
Kamil Budziewski Avatar answered Dec 05 '22 01:12

Kamil Budziewski