关于list
2024-09-06 22:08:08
//问题一:
List<string> list = new List<string>();
list = null;
//这样写可以使用,但是当list值为null时会报未将对象的引用设置到对象的实列
if (list.Count > )
{ }
//问题2:
List<string> list2 = null;
List<string> list4 = new List<string>();
list2 = list4;
//这样写可以使用,但是当list的count为0时同样为进入到if中
if (list2 != null)
{ }
//问题三:
//这样写,当if值为null时,list.Count > 0会报未将对象的引用设置到对象的实列
if (list.Count > || list != null)
{ } //问题四:
//这样写,会报未将对象的引用设置到对象的实列
List<string> events = null;
if (events != null || events.Count != )
{ }
else
{ } //解决方法:
List<string> list3 = new List<string>();
list3 = (list3 == null) ? new List<string>() : list3;//加个这个就可以直接使用list.Count > 0这种这种判断了,list值为null也没啥影响了
if (list.Count > )
{ }
//或者
if (list3 == null || list3.Count == )
{ }
else
{ }
最新文章
- 从无到有实现登录功能以及thinkphp怎么配置数据库信息
- Azure Redis Cache
- [转]自己写PHP扩展之创建一个类
- [CC]DgmOctree—执行Cell遍历和单元计算
- EMC Documentum DQL整理(三)
- git 教程(3)--时光机穿梭
- 原生 js 写分页
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
- RadioButton 的background属性表现特征
- 局域网内Tomcat服务器没法访问
- CodeFirst-数据迁移-Migration
- 抓取锁的sql语句-第六次修改
- java03变量和基本数据类型
- iOS 7用户界面过渡指南
- hdu 4506 快速幂
- HDU5616 天平能否称出物体重量问题 01背包变形或者折半搜索
- [UE4]Canvas Panel
- 原生js返回顶部
- 帝国cms底部代码哪里改?要修改版权和统计代码
- TextBox_TextChanged