Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion between Array,List,Dictionary and C #

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve Array,List,Dictionary conversion in C#". In daily operation, I believe many people have doubts about how to achieve Array,List,Dictionary conversion in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve Array,List,Dictionary conversion in C#". Next, please follow the editor to study!

First, the code example realizes the function.

Convert Array to List

Convert List to Array

Convert Array to Dictionary

Convert Dictionary to Array

Convert List to Dictionary

Convert Dictionary to List

Second, code implementation

Student category

Class Student {public int Id {get; set;} public string Name {get; set;} public string Gender {get; set;}}

Conversion implementation code

Static void Main (string [] args) {# region create student array / / create array Student [] StudentArray = new Student [3] / / create 3 student objects and assign values to each element of the array StudentArray [0] = new Student () {Id = 0001, Name = "Tony", Gender = "M"} StudentArray [1] = new Student () {Id = 0002, Name = "Hulk", Gender = "M"} StudentArray [2] = new Student () {Id = 0003, Name = "Black", Gender = "F"}; # endregion Console.WriteLine ("= test print information =") / / print Array student information Console.WriteLine ("print Array student information:"); foreach (Student student in StudentArray) {Console.WriteLine ("Id =" + student.Id + "" + "Name =" + student.Name + "" + "Gender =" + student.Gender) } / / change Array to LIST List StudentList = StudentArray.ToList (); / / print student information Console.WriteLine in List ("print List student information:") Foreach (Student student in StudentList) {Console.WriteLine ("Id =" + student.Id + "" + "Name =" + student.Name + "" + "Gender =" + student.Gender);} / / LIST to Array Student [] ListToArray = StudentList.ToArray (); Console.WriteLine ("print student information in ListToArray:") / / print student information foreach (Student student in ListToArray) {Console.WriteLine ("Id =" + student.Id + "" + "Name =" + student.Name + "" + "Gender =" + student.Gender) in ListToArray;} / / convert Array to Dictionary Dictionary StudentDictionary = StudentArray.ToDictionary (key = > key.Id, Studentobj = > Studentobj) / / print student information in ArrayToDictionary Console.WriteLine ("print student information in ArrayToDictionary:"); foreach (KeyValuePair student in StudentDictionary) {Console.WriteLine ("Id =" + student.Key + "" + "Name =" + student.Value.Name + "" + "Gender =" + student.Value.Gender) } / / convert Dictionary to Array Student [] DictionaryToArray = StudentDictionary.Values.ToArray (); / / print student information Console.WriteLine in Dictionary to Array ("print student information in DictionaryToArray:") Foreach (Student student in DictionaryToArray) {Console.WriteLine ("Id =" + student.Id + "" + "Name =" + student.Name + "" + "Gender =" + student.Gender);} / / List is converted to Dictionary Dictionary ListToDictionary = StudentList.ToDictionary (key = > key.Id, value = > value) / / print student information in ListToDictionary Console.WriteLine ("print student information in ListToDictionary:"); foreach (KeyValuePair student in ListToDictionary) {Console.WriteLine ("Id =" + student.Key + "" + "Name =" + student.Value.Name + "" + "Gender =" + student.Value.Gender) } / / convert Dictionary to List List DictionaryToList = StudentDictionary.Values.ToList (); / / print student information Console.WriteLine in DictionaryToList ("print student information in DictionaryToList:") Foreach (Student student in DictionaryToList) {Console.WriteLine ("Id =" + student.Id + "" + "Name =" + student.Name + "" + "Gender =" + student.Gender ");} Console.WriteLine (" = END= "); Console.ReadLine ();} III. Result output

At this point, the study on "how to convert Array,List,Dictionary to each other in C #" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report