关于list
//问题一:
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
{ }
随机推荐
- [蓝桥杯2015初赛]生命之树(树状dp)
在X森林里,上帝创建了生命之树.他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值.上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都存在一个点列 ...
- 【转】python中的闭包详细解析
一.什么是闭包? 如果一个内嵌函数访问外部嵌套函数作用域的变量,并返回这个函数,则这个函数就是闭包 闭包必须满足三个条件: 1. 必须有一个内嵌函数 2. 内嵌函数必须引用外部嵌套函数中的变量 ...
- 31 反射方式给类的属性赋值 和 对象赋值(clone)
1.配置类 package com.da.tool.util.configuration.reflect; /** */ public class JobInfo { private String j ...
- Tomcat 8 Invalid character found in the request target. The valid characters are defined in RFC 3986
终极解决方案: Invalid character found in the request target. The valid characters are defined in RFC 3986 ...
- Python 基础之推导式
一.列表推导式 通过一行循环判断,遍历出一系列数据的方式就是推导式 特点:方便,简洁,可以实现一些简单的功能推导式当中只能跟循环和判断(单项分支)种类分为三种: 列表推导式 集合推导式 字典推导式 ...
- YII insert multiple records into a table
$values = array(array(1,2),array(3,4),array(5,6),); $nbValues = count($values); $sql = 'INSERT INTO ...
- Genymotion连接失败问题
adb启动问题:Invalid argument: cannot open transport registration socketpair could not read ok from ADB S ...
- [蓝桥杯2015决赛]四阶幻方(DFS + 剪枝)
题目描述 把1~16的数字填入4x4的方格中,使得行.列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方. 四阶幻方可能有很多方案.如果固定左上角为1,请计算一共有多少种方案. 比如: 1 ...
- kafka-console-consumer接收不到flume推送过来的消息
原因和解决方法:需要先启动kafka,再启动flume,两者启动有先后顺序.
- windows系统下 VUE cli手脚架环境安装
1.安装 node.js环境 (cmd命令工具里输入 node -v 检测是否安装成功) 2.安装VUE 全局环境 npm install --global vue-cli (cmd命令工具里面安装 ...