//问题一:
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
{ }

随机推荐

  1. 笔记-Python-性能优化

    笔记-Python-性能优化 1.      开始 1.1.    python性能差么? 做一个判断前,先问是不是. python运行效率低是事实. 1.2.    为什么? 原因: Python是 ...

  2. Python学习第十课——文件的基本操作

    文件基本操作 文件读操作 #读出路径下的测试.txt文件 f = open('测试.txt', encoding='utf-8') # 打开要读文件 data = f.read() # 读取内容 pr ...

  3. StringUtils工具类中的isBlank()方法和isEmpty()方法的区别

    1.isBlank()方法 1 public static boolean isBlank(String str) { 2 int strLen; 3 if (str == null || (strL ...

  4. sqlalchemy 连接mysql8.0报 RuntimeError: cryptograpy si requeired for sha256_password 错误

    cryptography is required for sha256_password or caching_sha2_password 需要cryptography模块的支持才能连接需要sha25 ...

  5. Python 面试问答基础篇

    1.       Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对 ...

  6. 本周总结(19年暑假)—— Part2

    日期:2019.7.21 博客期:108 星期日 这几天正在认真学习大数据,我是在B站上看尚老师的视频搞得.我已经配好了Hadoop的基本环境,现在学习的是HDFS的相关内容

  7. SqlParameter 类

    SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间: System.Data.SqlClient 程序集: System ...

  8. 远程服务器使用tensorboard

    1 .由于服务器上tensorboard使用的端口是6006,因此,连接ssh时,将服务器的6006端口重定向到自己机器上的16006端口: ssh -L 16006:127.0.0.1:6006 u ...

  9. 配置solrcloud

    1.1   Zookeeper集群的搭建 1.1.1   前台条件 三个zookeeper实例.Zookeeper也是java开发的所以需要安装jdk. 1.Linux系统 2.Jdk环境. 3.Zo ...

  10. JDBC--批量处理

    1.当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理,这样可以提高处理速度. 2.JDBC的批量处理语句包括两个方法: --1)addBat ...