如果一个对象不为空null时,把它赋给另外一个对象:
像下面这个样子,需要把str的值赋给result,前提条件是在不为空null的前提之下:

class Aj
{
public void DemoNUll()
{
string str = null;
string result = "";
}
}
方法一:

if (str == null)
result = "";
else
result = str;
方法二:

if (str != null)
{
result = str;
}
方法三:

result = str == null ? "" : str;
方法四:

result = str ?? "";
以上这篇c# 判断是否为空然后赋值的4种实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。