C# 0 长度 List ToArray 的数组是长度为 0 不为 null 的数组

List<int> list = new List<int>();
int[] arr = list.ToArray();
if (arr != null)
{
   Console.WriteLine(arr.Length);
}

int[] arr1 = new int[0];
if (arr1 != null)
{
   Console.WriteLine(arr1.Length);
}

输出:

0
0

可见,可以 new 长度为 0 的数组。长度为 0 的 List 转数组,ToArray 的时候,就是长度为 0 的数组,且不为 null;

posted @   double64  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示