C#高级
 //字典类
            List<Dictionary<string, string>> dics = new List<Dictionary<string, string>>();
            Dictionary<string, string> pairs = new Dictionary<string, string>();
            pairs.Add("name1", "张三");
            pairs.Add("name2", "李四");
            dics.Add(pairs);
            List<string> vs = new List<string>();
            int f = 0;
            foreach (Dictionary<string, string> item in dics)
            {
                if (item.Keys.Contains("name1"))
                {
                    var testA = item.Values.ToList()[f];
                    vs.Add(testA);
                }
                f++;
            }
            //哈希表(Hashtable):
            Hashtable hashtable = new Hashtable();
            hashtable.Add(100, "西安");
            //能转换成功
            string hashvalue = hashtable[100] as string;
            if (hashvalue != null)
            {
                Console.WriteLine(hashvalue);
            }
            // 转换失败,获取的值为null,但不会抛出错误。
            StreamReader reader = hashtable[100] as StreamReader;
            if (reader == null)
            {
                Console.WriteLine("西安不是StreamReader型");
            }
            // 也可以直接获取object值,再做判断
            object value2 = hashtable[100];
            if (value2 is string)
            {
                Console.Write("这个是字符串类型: ");
                Console.WriteLine(value2);
            }
            //找出每个集合中都包含的元素:
            Console.WriteLine("找出每个集合中都包含的元素:");
            List<string[]> listA = new List<string[]>();
            string[] itemA = { "1", "2", "3", "4", "5" };
            string[] itemB = { "1", "5", "2" };
            string[] itemC = { "2", "3", "7", "5" };
            string[] itemD = { "4", "5", "8", "3", "2" };
            listA.Add(itemA);
            listA.Add(itemB);
            listA.Add(itemC);
            listA.Add(itemD);
            string[] containArray = new string[] { };
            for (int i = 0; i < listA.Count - 1; i++)
            {
                if (i == 0)
                {
                    containArray = listA[i];
                }
                containArray = containArray.Where(x => listA[i + 1].Contains(x)).ToArray();
            }
            foreach (var item in containArray)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\n");
            //集合(GroupBy):
            List<DbInfo> infos = new List<DbInfo>()
            {
                new DbInfo(){ DbId=1,HostId=1 },
                new DbInfo(){ DbId=2,HostId=2 },
                new DbInfo(){ DbId=3,HostId=1 },
                new DbInfo(){ DbId=4,HostId=2 },
                new DbInfo(){ DbId=5,HostId=1 },
                new DbInfo(){ DbId=6,HostId=2 },
                new DbInfo(){ DbId=7,HostId=1 },
                new DbInfo(){ DbId=8,HostId=3 },
            };
            //分组统计
            //var result = infos.GroupBy(d => d.HostId); 
            //var ee = result.Where(d => d.Key == 1).FirstOrDefault() == null ? 0 : result.Where(d => d.Key == 1).FirstOrDefault().Count();
            //Console.WriteLine(ee);
            //Console.ReadLine();
            //分组前
            Console.WriteLine("分组前:");
            foreach (var item in infos)
            {
                Console.WriteLine(" HostId:" + item.HostId + " DbId:" + item.DbId);
            }
            //分组后
            Console.WriteLine("分组后:");
            List<DbInfo> dbInfos = new List<DbInfo>();
            var result = infos.ToLookup(d => d.HostId);
            foreach (var item in result)
            {
                var info = item.FirstOrDefault();
                DbInfo dbInfo = new DbInfo();
                dbInfo.HostId = info.HostId;
                dbInfo.Infos = item.ToList();
                dbInfos.Add(dbInfo);
            }
            foreach (var item in dbInfos)
            {
                Console.WriteLine("HostId:" + item.HostId);
                string arr = "[\n";
                foreach (var info in item.Infos)
                {
                    arr += " HostId:" + info.HostId + " DbId:" + info.DbId + "\n";
                }
                arr += "]";
                Console.WriteLine(arr);
            }
            Console.ReadLine();
            //赋值写法(各种数据类型):
            int[] intA = { 0, 1, 2 };
            string[] strA = { "0", "1", "2" };
            List<int> intList = new List<int> { 0, 1, 2 };
            List<string> strList = new List<string> { "0", "1", "2" };
            Entities aa = new Entities { Name = "Higa, Sidney", Pets = new List<string> { "0", "1", "2" } };
            List<Student> list = new List<Student>
            {
                new Student{Name="YJingLee",Age="18"},
                new Student{Name="YJingLer",Age="19"},
             };
            list.ForEach(x =>
            {
                Console.WriteLine(x.Name + " " + x.Age);
            });
            List<PersonA> aList = new List<PersonA>() {
                new PersonA("张三", "18"),
                new PersonA("张四", "19"),
                new PersonA("张五", "20"),
                new PersonA("张六", "21")
            };
            foreach (var item in aList)
            {
                Console.Write(item.Name + "-" + item.Age);
                Console.WriteLine();
            }
            //合并多个集合到一个:
            Entities[] petOwners =
            {
                new Entities {Name = "张三", Pets = new List<string> {"11", "11"}},
                new Entities {Name = "李四", Pets = new List<string> {"22", "22"}},
                new Entities {Name = "王五", Pets = new List<string> {"33", "33"}}
            };
            IEnumerable<string> resultA = petOwners.SelectMany(petOwner => petOwner.Pets);
            foreach (string pet in resultA)
            {
                Console.WriteLine(pet);
            }
            Console.WriteLine("\n");
            //Dictionary<int,string>类型:
            Console.WriteLine("Dictionary<int,string>泛型");
            Dictionary<int, string> myDictionary = new Dictionary<int, string>();
            myDictionary.Add(0, "C#");
            myDictionary.Add(1, "C++");
            myDictionary.Add(2, "C");
            myDictionary.Add(3, "VB");
            if (myDictionary.ContainsKey(0))
            {
                Console.WriteLine("Key:{0},Value:{1}", 0, myDictionary[0]);
            }
            foreach (KeyValuePair<int, string> kvp in myDictionary)
            {
                Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
            }
            Dictionary<int, string>.KeyCollection keyCol = myDictionary.Keys;
            foreach (int key in keyCol)
            {
                Console.WriteLine("Key = {0}", key);
            }
            Dictionary<int, string>.ValueCollection valueCol = myDictionary.Values;
            foreach (string value in valueCol)
            {
                Console.WriteLine("Value = {0}", value);
            }
            //移除指定的键值By Remove方法
            myDictionary.Remove(0);
            if (myDictionary.ContainsKey(0))
            {
                Console.WriteLine("Key:{0},Value:{1}", 0, myDictionary[0]);
            }
            else
            {
                Console.WriteLine("不存在 Key : 0");
            }
            Console.WriteLine("\n");
            //计算程序运行时间:
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Thread.Sleep(3000);
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
            Console.WriteLine("\n");
            //反射:
            Student nc = new Student();
            Type t = nc.GetType();
            PropertyInfo[] pis = t.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                Console.WriteLine("反射:" + pi.Name);
            }
            Console.WriteLine("\n");
            //查看类中的构造方法:
            nc = new Student();
            t = nc.GetType();
            ConstructorInfo[] ci = t.GetConstructors(); //获取类的所有构造函数
            foreach (ConstructorInfo c in ci) //遍历每一个构造函数
            {
                ParameterInfo[] ps = c.GetParameters();    //取出每个构造函数的所有参数
                foreach (ParameterInfo pi in ps)   //遍历并打印所该构造函数的所有参数
                {
                    Console.Write(pi.ParameterType.ToString() + " " + pi.Name + ",");
                }
                Console.WriteLine();
            }
            //字段赋值
            nc = new Student();
            t = nc.GetType();
            object obj = Activator.CreateInstance(t);
            //取得ID字段 
            FieldInfo fi = t.GetField("Id");
            //给ID字段赋值 
            fi.SetValue(obj, "k001");
            //取得MyName属性 
            PropertyInfo pi1 = t.GetProperty("Name");
            //给MyName属性赋值 
            pi1.SetValue(obj, "grayworm", null);
            PropertyInfo pi2 = t.GetProperty("content");
            pi2.SetValue(obj, "hi.baidu.com/grayworm", null);
            //取得show方法 
            MethodInfo mi = t.GetMethod("Show");
            //调用show方法 
            var invoke = mi.Invoke(obj, null);
            //输出show的内容
            Console.WriteLine(invoke);
            Console.WriteLine("\n");
            //C#映射:
            var person = new PersonA("张三", "12", "男");
            var student = new Student();
            //第一个类型转成第二个类型
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PersonA, Student>();
            mapper.Map(person, student);
            Console.WriteLine("姓名:" + student.Name + ",年龄:" + student.Age);
                    
                
                
            
        
浙公网安备 33010602011771号