網站首頁 編程語言 正文
一、代碼實例實現功能
- 將Array轉換為List
- 將List轉換為Array
- 將Array轉換為Dictionary
- 將Dictionary轉換為Array
- 將List轉換為Dictionary
- 將Dictionary轉換為List
二、代碼實現
?學生類
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
?轉換實現代碼
static void Main(string[] args)
{
#region 創建學生數組
//創建數組
Student[] StudentArray = new Student[3];
//創建創建3個student對象,并賦值給數組的每一個元素
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("=================測試打印信息=================");
//打印Array中學生信息
Console.WriteLine("打印Array中學生信息:");
foreach (Student student in StudentArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//Array轉為LIST
List<Student> StudentList = StudentArray.ToList<Student>();
//打印List中的學生信息
Console.WriteLine("打印List中學生信息:");
foreach (Student student in StudentList)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//LIST轉為Array
Student[] ListToArray = StudentList.ToArray<Student>();
Console.WriteLine("打印ListToArray中的學生信息:");
//打印ListToArray中的學生信息
foreach (Student student in ListToArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//Array轉換為Dictionary
Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id, Studentobj => Studentobj);
//打印ArrayToDictionary中的學生信息
Console.WriteLine("打印ArrayToDictionary中的學生信息:");
foreach (KeyValuePair<int, Student> student in StudentDictionary)
{
Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
}
//Dictionary轉換為Array
Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
//打印Dictionary轉Array中的學生信息
Console.WriteLine("打印DictionaryToArray中的學生信息:");
foreach (Student student in DictionaryToArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//List轉換為Dictionary
Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);
//打印ListToDictionary中的學生信息
Console.WriteLine("打印ListToDictionary中的學生信息:");
foreach (KeyValuePair<int, Student> student in ListToDictionary)
{
Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
}
//Dictionary轉換為List
List<Student> DictionaryToList = StudentDictionary.Values.ToList();
//打印DictionaryToList中的學生信息
Console.WriteLine("打印DictionaryToList中的學生信息:");
foreach (Student student in DictionaryToList)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
Console.WriteLine("===============END===================");
Console.ReadLine();
}
三、結果輸出
原文鏈接:https://www.cnblogs.com/wml-it/p/15514456.html
相關推薦
- 2022-06-20 Python基于DFA算法實現內容敏感詞過濾_python
- 2022-03-25 Redis分布式鎖如何實現續期_Redis
- 2022-12-13 C++實現字符格式相互轉換的示例代碼_C 語言
- 2022-03-31 python中常用的九個語法技巧_python
- 2022-06-22 git工作區暫存區與版本庫基本理解及提交流程全解_其它綜合
- 2023-02-27 pandas?pd.cut()與pd.qcut()的具體實現_python
- 2022-04-15 關于pyinstaller生成.exe程序報錯:缺少.ini文件的分析_python
- 2022-05-24 C#創建及訪問網絡硬盤的實現_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支