Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

other way to check if value is null or empty and assign value

Tags:

c#

asp.net

 if (!string.IsNullOrEmpty(stringTest))
           newString = stringTest; // newString is a string
 else
           newString = string.Empty;

Is there another (simple) way to achieve this?

like image 309
Florin M. Avatar asked Jan 30 '26 12:01

Florin M.


1 Answers

You need the Null-coalescence operator:

newString = stringTest ?? String.Empty;
like image 112
JeffRSon Avatar answered Feb 01 '26 02:02

JeffRSon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!