C# List, Array, Dictionary相互转换

  • 将Array转换为List
  • 将List转换为Array
  • 将Array转换为Dictionary
  • 将Dictionary 转换为Array
  • 将List转换为Dictionary
  • 将Dictionary转换为List
    首先,我们定义一个“Student”类,它有三个自动实现属性。
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
    }        

1. Array转换为List

将数组转换成一个List,我先创建了一个student类型的数组。

        static void Main(string[] args)
        {
            Student[] studentArray = new Student[3];
            studentArray[0] = new Student()
            {
                Id = 12,
                Name = "Lucky",
                Gender = "Male"
            };
            studentArray[1] = new Student()
            {
                Id = 23,
                Name = "Poppy",
                Gender = "Male"
            };
            studentArray[2] = new Student()
            {
                Id = 55,
                Name = "Jack",
                Gender = "Female"
            };
            //foreach循环数组
            foreach (Student student in studentArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + "  " + " Gender = " + student.Gender);
            }                

            //1. 数组转成List
            List<Student> StudentList = studentArray.ToList<Student>();
            foreach (Student student in StudentList)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

2. List转换为Array

            Student[] ListToArray = StudentList.ToArray<Student>();
            foreach (Student student in ListToArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

3. Array转Dictionary

            Dictionary<int, Student> StudentDictionary = studentArray.ToDictionary(key=>key.Id, Studentobj => Studentobj);
            foreach (KeyValuePair<int, Student> student in StudentDictionary)
            {
                Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
            }

4. Dictionary转Array

            Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
            foreach (Student student in DictionaryToArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

5. List转Dictionary

            Dictionary<int, Student> ListToDictionary = ListToArray.ToDictionary(key => key.Id, value => value);
            foreach (KeyValuePair<int, Student> student in ListToDictionary)
            {
                Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
            }

6. Dictionary转List

            List<Student> DictionaryToList = StudentDictionary.Values.ToList();
            foreach (Student student in DictionaryToList)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

原文地址:https://www.cnblogs.com/LuckyZLi/p/10777066.html

时间: 2024-10-09 15:19:53

C# List, Array, Dictionary相互转换的相关文章

Day1 (let and var)/(String,Int,Float,Double)/(Tuples,contains: array,dictionary)

let secondSystem:Int = 0b10001 let eightSystem:Int  = 0o21 let tenSystem:Int    = 17 let sixthSystem:Int  = 0x11 // summary: 1.u need insert a "Enter" behind equal symbol let num_a:Float = 1 let num_b:Int = 1 let a:Int = 3 let b:Double = 0.14159

for 遍历 Dictionary

使用for来遍历Dictionary ToArray方法: ToArray等于把Dictionary拷贝了一份   使用Linq的方法 Enumerable.ElementAt<TSource>  (IEnumerable<TSource>, Int32) 参考:https://msdn.microsoft.com/zh-cn/library/bb299233%28v=vs.110%29.aspx   方法代码: public static void Main(string[] a

Dictionary of js

function Dictionary() { this.datastore = new Array(); }; Dictionary.prototype = { constructor: Dictionary, add: function(key, value) { this.datastore[key] = value; }, find: function(key) { return this.datastore[key]; }, remove: function(key) { delete

IOS学习之路——Swift语言(2)——基本类型与函数

一.可选型 为什么会有可选型?当一个变量被希望表示为某一个指定的类型值,同时也被希望在某种情况下表示为空.那么就需要一个可选型.Optional 可选型的语法是在Swift的基本类型后面添加 ? 符号,如 Int? String?  可选型如果取值为空,那么表示为 nil 如果想定义一个可选型的变量,那么需要显示的声明为可选型.可选型变量打印时显示Optional 可选型变量需要进行解包(unwrap)才能与其他的变量进行运算. 解包: 1. 强制解包,使用 ! 关键字,强制解包是具有风险的,当

Swift学习 (四)

5.枚举与结构体: 不必给枚举成员提供一个值.如果我们想要为枚举成员提供一个值(raw value),我们可以用字符串,字符,整型或浮点数类型. 1 2 3 4 5 6 7 enum CompassPoint { case North case South case East case West   }   var directionToHead = CompassPoint.West 结构体 Struct在Swift中和类有许多相同的地方,可以定义属性,方法,初始化方法,可通过extensio

Swift学习 (一)

以后会自己总结学习Swift的笔记与深化.希望能够帮助已经有Objective-C经验的开发者更快地学习Swift.我们一起学习,同时也品味到Swift的精妙之处. 结论放在开头:我认为Swift比Objective-C更优雅,更安全同时也更现代,更性感. 首先学习从Objective-C到Swift的语法差异.我们熟悉的Objective-C特性在Swift中如何展现: 1.属性(property)和实例变量(instance variable) Objective-C property in

iOS开发——学习总结swift篇&amp;swift 2.0学习与总结一

swift 2.0学习与总结一 一:属性策略(OC中的叫法) strong: 在Swift中是默认的 weak: 通过weak关键词申明 weak var delegate: UITextFieldDelegate? readonly,readwrie 直接通过声明变量var,声明常量let的方式来指明 copy 通过@NSCopying指令声明. 值 得注意的是String,Array和Dictionary在Swift是以值类型(value type)而不是引用类型(reference typ

ios开发 数据存储

原地址:http://blog.csdn.net/sxhong/article/details/8223881 首选项设置存储 NSUserDefaults 以及通过它控制的SettingBundle  NSUserDefaults 用来保存一些设置,他会自动写到何时的位置.NSUbiquitousKeyValueStore 多平台同步设置,限制是大小64k,开启entitlement,唯一Apple ID(还要考虑无法连接到iCloud网络时的情形) 文件存储一.目录描述<Applicatio

Object-C内存管理的理解总结

今天看到了OC的内存管理这块,觉得很亲切. 自己的习惯是尽量自己掌控程序的空间和时间,有点强迫症的感觉.用C和C++做项目的时候,时时刻刻都在操心这new和delete的配对使用和计数,学习stl和boost的时候看到了智能指针等时候,依然不是很爱使用,还是愿意坚持自己控制new和delete:后来用C#后,一直特别注意Dispose相关的动作,尽早释放对象占有的内存空间,避免无谓的占用一直到程序退出才释放. OC中系统对每个实例对象地址都记录一个引用次数(当然有特例,见另外一篇随笔),这就是引