سؤال

Guess I got this code,

string a, b;
b = null;

How can I use "?" operator to check if b is not null or empty.

I want to get value of "b" if it's not null or empty in "a"

I don't want to use, string.IsNullOrEmpty(), Reason ---> I don't want to use "if and else" :)

Let me guess your next question, why don't want to use if and else.

هل كانت مفيدة؟

المحلول

This would work:

a = (b ?? "") == "" ? a : b;

But why on earth not just use this:

a = string.IsNullOrEmpty(b) ? a : b;

There's no need to resort to if and else with this...

نصائح أخرى

You could do this:

a = b == null || b == string.Empty ? "Some Value" : b;

Of course, you can always just do this:

a = string.IsNullOrEmpty(b) ? "Some Value" : b;

Using string.IsNullOrEmpty does not mean you have to use a if / else-block

Is that what you are looking for : a = (b == null || b.Length < 1 ? a : b); ?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top