首页 > 编程语言 >C#|在List集合为NULL时需要添加数据的处理方法

C#|在List集合为NULL时需要添加数据的处理方法

时间:2022-09-02 12:47:08浏览次数:84  
标签:index C# List dic NEAR FID NULL tempList

最近写了一个循环往字典的Value值添加数据的程序(字典的Value为一个List),经常碰到 “Object reference not set to an instance of an object”,就自己去了解了一下空集合和集合为NULL的区别,可以看看这篇文章《list集合为空或为null的区别》

理解区别之后,我们来看一下原来错误的代码:

Dictionary<int, List<string> >dic = new Dictionary<int, List<string>>();
int rowCount = ret_res.Rows.Count;
for (int index = 0; index < rowCount; index++)
{
    List<string> NewList = new List<string>();
    int NEAR_FID = Convert.ToInt32(ret_res.Rows[index]["NEAR_FID"]);
    if (dic.TryGetValue(NEAR_FID, out NewList))
    {
        List<string> tempList = new List<string>();
        tempList = dic[NEAR_FID];
        int tempnum = int.Parse(tempList.First());
        tempnum++;
        string IN_FID = ret_res.Rows[index]["IN_FID"].ToString();
        tempList[0] = tempnum.ToString();
        tempList.Add(IN_FID);
        dic[NEAR_FID] = tempList;
        //dic.Add(NEAR_FID, new List<string>(tempList));
        tempList.Clear();
    }
    else
    {
        int num = 0;
        num++;
        string IN_FID = ret_res.Rows[index]["IN_FID"].ToString();
        string strnum = num.ToString();
        NewList.Add(IN_FID);
        dic.Add(NEAR_FID, new List<string>(NewList));
        NewList.Clear();
    }
}

运行之后报错如下:

显示Object reference not set to an instance of an object(对象引用没有设置为一个对象的实例),list是null。这种情况出现的原因是因为List集合为null的时候相当于不存在,所以添加不了元素。用日常生活举例,当你找不到一个容器的时候,你怎么能往容器里装东西呢?有非常多的解决方法。我使用下面的方法解决:

Dictionary<int, List<string> >dic = new Dictionary<int, List<string>>();
int rowCount = ret_res.Rows.Count;
for (int index = 0; index < rowCount; index++)
{
    List<string> NewList = new List<string>();
    int NEAR_FID = Convert.ToInt32(ret_res.Rows[index]["NEAR_FID"]);
    if (dic.TryGetValue(NEAR_FID, out NewList))
    {
        List<string> tempList = new List<string>();
        tempList = dic[NEAR_FID];
        int tempnum = int.Parse(tempList.First());
        tempnum++;
        string IN_FID = ret_res.Rows[index]["IN_FID"].ToString();
        tempList[0] = tempnum.ToString();
        tempList.Add(IN_FID);
        dic[NEAR_FID] = tempList;
        //dic.Add(NEAR_FID, new List<string>(tempList));
        tempList.Clear();
    }
    else
    {
        int num = 0;
        num++;
        string IN_FID = ret_res.Rows[index]["IN_FID"].ToString();
        string strnum = num.ToString();
        var _list = new List<string>();
        _list.Add(strnum);
        NewList = _list;
        NewList.Add(IN_FID);
        dic.Add(NEAR_FID, new List<string>(NewList));
        NewList.Clear();
    }
}

大概思路是,我创建了类型相同的临时List集合,变量名为“_list”,用来添加字符串“strnum” ,然后再把添加完成的临时List集合“_list”赋值给为null的List集合“list”,这样就完成了为null的List集合的元素的添加。

 

标签:index,C#,List,dic,NEAR,FID,NULL,tempList
From: https://www.cnblogs.com/tangjielin/p/16649359.html

相关文章

  • 【C++】断言、likely等
    断言assert就是对表达式进行判断,如果条件不成立就会调用abort()中止程序运行,对于debug空指针有奇效,但是release版本不会用是一个宏而非函数五个要点:1.在函数开始时,监测......
  • php webman对接Luckysheet share多人协作模式项目
    2022年9月2日10:05:03gitee地址https://gitee.com/zxadmin/luckysheet-phpserver此项目目前定义为对接失败,当然Luckysheet也可以去除掉加密的部分,直接json通信就没问题......
  • LC557
    stringreverseWords(strings){intl=0;intr=0;for(inti=0;i<s.length();i++){if(s[i]==''){r=i-1;while(l<r){......
  • LC344
    stringreverseWords(strings){intl=0;intr=0;for(inti=0;i<s.length();i++){if(s[i]==''){r=i-1;while(l<r){......
  • C#|Dictionary 内含有 List 的添加操作
    最近在循环中给字典中的列表进行添加操作时,发现直接通过一个列表添加是不可行的,需要创建新的列表才可以,还是比较好理解的,如下所示:Dictionary<string,List<string>>myDic......
  • cmake是什么,为什么现在都用cmake,cmake编译原理和跨平台示例
    一cmake是什么? CMake是一个开源、跨平台的工具系列,是用来构建、测试和打包软件。CMake使用平台无关的配置文件来控制软件编译过程,并生成可在您选择的编译器环境中使用......
  • IfcExternalReference
    IfcExternalReference实体定义IfcExternalReference是当前模型或项目数据库中未明确表示的信息的标识(作为当前模型的实现)。这些信息可以包含在分类、文档或库中。IfcExte......
  • CF446C DZY Loves Fibonacci Numbers
    CF446CDZYLovesFibonacciNumbers题目大意在本题中,我们用\(f_i\)来表示第\(i\)个斐波那契数(\(f_1=f_2=1,f_i=f_{i-1}+f_{i-2}(i\ge3)\))。维护一个序列\(a\),长......
  • 查看Oracle当前用户下的(表,视图,同义词,索引等...)
     copy自:查看Oracle当前用户下的(表,视图,同义词,索引等...)表空间–查看当前用户表空间selectusername,default_tablespacefromuser_users;selectdefault_tablespace......
  • SpringCloud 使用 OpenFeign 声明式服务调用
    Feign组件最初由Netflix公司提供,由于不支持SpringMVC注解,所以SpringCloud对其封装并进行支持,因此产生了OpenFeign组件。Feign是一个声明式的REST客户端,它采用......