欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

C# List 嵌套学习总结

发布时间:2025/4/14 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C# List 嵌套学习总结 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

C#的List类型如何嵌套

List<List<string>> a = new List<List<string>>(); 这样用肯定就会报错。 List<(System.Object)

List<string>> a = new List<(System.Object)List<string>>(); 这样会不会报错不清楚,进行了装箱

操作。不过这种方法比较土。可能还有更好的方法吧? 

List<List<string>> a = new List<List<string>>();
怎么会报错呢??
语法上是行得通的呀~
你是不是给a添加的元素不是List<string>类型的呀,再或者添加a的元素没有new?
以下测试代码
List<List<string>> a = new List<List<string>>();List<string> firstElement = new List<string>();firstElement.Add("ABC");List<string> secondElement = new List<string>();secondElement.Add("BCD");a.Add(firstElement);a.Add(secondElement);foreach (List<string> i in a){foreach (string s in i){Console.WriteLine(s);}}
Dictionary<List<string>,List<string>> id=new Dictionary<List<string>,List<string>>();想怎么

嵌套都行。
循环用foreach (KeyValuePair<string,string> item in id)
{
……
}
========

C# List<T>的嵌套和foreach的使用

http://blog.csdn.net/hwulong/article/details/53957243  

关于C#中List<T>的概念,可以和高中数学的集合概念进行对比理解,List<T>的嵌套可以理解为元素是

集合的集合,用高中数学的集合的概念来表示就是{{0,1,2,3},{4,5,6,7},{8,9,10,11}}。用程序语言

来表示如下:

先声明一个元素为集合的集合myList,然后在声明几个元素为int类型的集合,最后用add方法将myList1

,myList2,myList3,添加到myList中。
    关于foreach的使用,最开始对其概念和运行过程不是很了解,写了个遍历集合myList的代码,想在

控制台中输出0,1,2,3,4,5,6,7,8,9,10,11,可运行的结果并非如此:

百度了一下foreach的用法和单步调试程序,终于搞懂了foreach的运行过程:foreach循环用于列举出集

合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成。in右边的项是集合名,in左边

的项是变量名,用来存放该集合中的每个元素。该循环的运行过程如下:每一次循环时,从集合中取出

一个新的元素值。放到只读变量中去,如果括号中的整个表达式返回值为true,foreach块中的语句就能

够执行。一旦集合中的元素都已经被访问到,整个表达式的值为false,控制流程就转入到foreach块后

面的执行语句。重写了foreach程序,终于得到了自己想要的结果!

该程序运行过程如下:集合myList为{{0,1,2,3},{4,5,6,7},{8,9,10,11}},里面共有3个类型为集合

的元素{0,1,2,3},{4,5,6,7},{8,9,10,11},foreach第一次运行的时候,把myList的第一个元素

{0,1,2,3}放到变量item中去,这个item为集合类型的变量,item[0]=0,item[1]=1,item[2]=2,item

[3]=3。foreach第二次运行的时候,把myList的第二个元素{4,5,6,7}放到变量item中去,这时item[0]

=4,item[1]=5,item[2]=6,item[3]=7。foreach第三次运行的时候,把myList的第三个元素

{8,9,10,11}放到变量item中去,这时item[0]=8,item[1]=9,item[2]=10,item[3]=11。
========

C# list嵌套定义赋值

http://www.kwstu.com/ArticleView/kwstu_2014499823494

简单记录一下c# list嵌套定义及赋值的方法:
定义:

//展位列表
public class zwListViewModel
{
    public string ID { get; set; }
    public string ZWNAME { get; set; }
    public string BOOKQYID { get; set; }
    public string BOOKQY { get; set; }
    public string PONAMELIST { get; set; }
    public List<poListViewModel> poList { get; set; }
}
//职位列表
public class poListViewModel
{
    public string POID { get; set; }
    public string PONAME { get; set; }
}
赋值方法:
?

var listZw = new zwListViewModel();
List<poListViewModel> poList = new List<poListViewModel>();
poListViewModel tmp = new poListViewModel();
tmp.POID = "ID";
tmp.PONAME = "NAME";
poList.Add(tmp);
listZw.poList = poList;
========

C#遍历Object各个属性含List泛型嵌套

http://www.cnblogs.com/sword85/p/4490975.html
   同事遇到一个问题:在做手机app接口时,返回JSON格式,json里面的数据属性均是string类型,但

不能出现NULL(手机端那边说处理很麻烦,哎)。Model已经创建好了,而且model的每个属性均是

string类型。数据层使用EF。数据库也有些字段可为空。这时,需要大量的验证属性是否为NULL,并将

属性值为NULL的转换成"".

   解决方案:1遍历model各个属性,当为NULL时,赋值"".2.支持泛型List<model>的嵌套。

  前提条件:model的值只有这几种,List<model> ,string ,多层嵌套。

  于是写了如下代码遍历属性,遇到很多问题,初稿,临时用,后面完善。

/// <summary>
/// 去除model属性为null 的情况,把null改成""。。该方法仅用在属性均为string类型的情况,主要

用于手机APP。 chj 2015-5-7 17:39:21
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="inputModel"></param>
/// <returns></returns>
public static object CJRemoveNULLByRecursive(object obj)
{
    Type t = obj.GetType();
    var typeArr = t.GetProperties();
    object tempItem;//应对属性含有参数时。
    if (obj != null )
    {
        foreach (var pi in typeArr)
        { 
            //当属性为字符串时
            if (pi.PropertyType == typeof(string))
            {
                if (pi.GetValue(obj, null)==null)
                {
                     pi.SetValue(obj, "", null);
                }
            }
            //当该属性为List泛型时,或者为引用类型,数组时。这里好像有个属性可以直接判断
            else if(pi.PropertyType.IsGenericType||pi.PropertyType.IsArray||
pi.PropertyType.IsClass)//.GetType()=typeof(Nullable))
            {
              var  paras=  pi.GetIndexParameters(); //索引化属性的参数列表
              if (paras.Count()> 0)
              {
                  int i = 0;
                  tempItem = pi.GetValue(obj, new object[] { 0 }); 
                  while (tempItem!=null)
                  {
                      pi.SetValue(obj, CJRemoveNULLByRecursive(tempItem), new object[] { i 
});
                      i++;
                      try
                      {
                         tempItem = pi.GetValue(obj, new object[] { i }); 
                      }
                      catch (Exception)
                      {
                          break;
                      }
                  }
              }
              else
              {
                  pi.SetValue(obj, CJRemoveNULLByRecursive(pi.GetValue(obj, null)), null);
              }
            }
              
        }
    }
    else
    {
        return "";
    }
    
    return obj;
}
  由于可能嵌套多层,使用递归。

========

总结

以上是生活随笔为你收集整理的C# List 嵌套学习总结的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。