国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# List 嵌套學習總結

2019-11-06 06:27:00
字體:
來源:轉載
供稿:網友

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;}  由于可能嵌套多層,使用遞歸。========
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 花莲市| 苍梧县| 西安市| 青川县| 融水| 登封市| 仪陇县| 安塞县| 道孚县| 安庆市| 锦屏县| 建湖县| 新安县| 增城市| 汕头市| 桐庐县| 平遥县| 大同县| 彭阳县| 监利县| 安新县| 延安市| 静安区| 灵川县| 达尔| 吕梁市| 达州市| 定西市| 武清区| 太谷县| 新昌县| 汾阳市| 屏东县| 稻城县| 德清县| 宿松县| 北京市| 肥乡县| 凤凰县| 昭觉县| 凤城市|