1 "“和null的区别 - null不分配内存空间,”“分配长度为空字符串的内存空间。
2 string.Empty和null的区别 - null不分配内存空间,string.Empty分配长度为空字符串的内存空间。
3 string.Empty和”“的区别 - string.Empty和”“类似,string.Empty是对”"的优化。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Demo : MonoBehaviour
{
string str1 = "";
string str2 = string.Empty;//string.Empty和""类似,string.Empty是对""的优化
string str3 = null;
string str4;//这句相当于string str2 = null;
void Start()
{
nullTest();
}
public void nullTest()
{
if (str1 == null)
{
Debug.Log("str1为null");
}
if (str2 == null)
{
Debug.Log("str2为null");
}
if (str3 == null)
{
Debug.Log("str3为null");
}
if (str4 == null)
{
Debug.Log("str4为null");
}
//常用检测""或空的方法
if (string.IsNullOrEmpty(str1)) {
Debug.Log("str1为null或为Empty");
}
}
}