문제

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