如果一个对象不为空null时,把它赋给另外一个对象:
像下面这个样子,需要把str的值赋给result,前提条件是在不为空null的前提之下:
class="code_img_closed" src="/Upload/Images/2017120812/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('bc35d2e9-3ef8-4aaa-b8b9-2014c19e2eb9',event)" src="/Upload/Images/2017120812/2B1B950FA3DF188F.gif" alt="" />
class Aj { public void DemoNUll() { string str = null; string result = ""; } }Source Code
方法一:
if (str == null) result = ""; else result = str;Source Code
方法二:
if (str != null) { result = str; }Source Code
方法三:
result = str == null ? "" : str;Source Code
方法四:
result = str ?? "";
Source Code