自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇學會 C# 集合類型

開發(fā) 后端
對于許多應用程序,你會想要創(chuàng)建和管理相關對象的組。有兩種方法對對象進行分組:通過創(chuàng)建對象的數(shù)組,以及通過創(chuàng)建對象的集合。

[[439433]]

 對于許多應用程序,你會想要創(chuàng)建和管理相關對象的組。有兩種方法對對象進行分組:通過創(chuàng)建對象的數(shù)組,以及通過創(chuàng)建對象的集合。

數(shù)組最適用于創(chuàng)建和使用固定數(shù)量的強類型化對象。

集合提供更靈活的方式來使用對象組。與數(shù)組不同,你使用的對象組隨著應用程序更改的需要動態(tài)地放大和縮小。對于某些集合,你可以為放入集合中的任何對象分配一個密鑰,這樣你便可以使用該密鑰快速檢索此對象。

集合是一個類,因此必須在向該集合添加元素之前,聲明類的實例。

如果集合中只包含一種數(shù)據(jù)類型的元素,則可以使用 System.Collections.Generic 命名空間中的一個類。泛型集合強制類型安全,因此無法向其添加任何其他數(shù)據(jù)類型。當你從泛型集合檢索元素時,你無需確定其數(shù)據(jù)類型或?qū)ζ溥M行轉(zhuǎn)換。

創(chuàng)建字符串列表,并通過使用 foreach 語句循環(huán)訪問字符串。

  1. // Create a list of strings. 
  2. var salmons = new List<string>(); 
  3. salmons.Add("chinook"); 
  4. salmons.Add("coho"); 
  5. salmons.Add("pink"); 
  6. salmons.Add("sockeye"); 
  7.  
  8. // Iterate through the list. 
  9. foreach (var salmon in salmons) 
  10.     Console.Write(salmon + " "); 
  11. // Output: chinook coho pink sockeye 

如果集合中的內(nèi)容是事先已知的,則可以使用集合初始值設定項來初始化集合。

  1. // Create a list of strings by using a 
  2. // collection initializer. 
  3. var salmons = new List<string> { "chinook""coho""pink""sockeye" }; 
  4.  
  5. // Iterate through the list. 
  6. foreach (var salmon in salmons) 
  7.     Console.Write(salmon + " "); 
  8. // Output: chinook coho pink sockeye 

可以使用 for 語句,而不是 foreach 語句來循環(huán)訪問集合。通過按索引位置訪問集合元素實現(xiàn)此目的。元素的索引開始于 0,結(jié)束于元素計數(shù)減 1。

以下示例通過使用 for 而不是 foreach 循環(huán)訪問集合中的元素。

  1. // Create a list of strings by using a 
  2. // collection initializer. 
  3. var salmons = new List<string> { "chinook""coho""pink""sockeye" }; 
  4.  
  5. for (var index = 0; index < salmons.Countindex++) 
  6.     Console.Write(salmons[index] + " "); 
  7. // Output: chinook coho pink sockeye 

對于 List中的元素類型,還可以定義自己的類。在下面的示例中,由 List使用的 Galaxy 類在代碼中定義。

  1. private static void IterateThroughList() 
  2.     var theGalaxies = new List<Galaxy> 
  3.         { 
  4.             new Galaxy() { Name="Tadpole", MegaLightYears=400}, 
  5.             new Galaxy() { Name="Pinwheel", MegaLightYears=25}, 
  6.             new Galaxy() { Name="Milky Way", MegaLightYears=0}, 
  7.             new Galaxy() { Name="Andromeda", MegaLightYears=3} 
  8.         }; 
  9.  
  10.     foreach (Galaxy theGalaxy in theGalaxies) 
  11.     { 
  12.         Console.WriteLine(theGalaxy.Name + "  " + theGalaxy.MegaLightYears); 
  13.     } 
  14.  
  15.     // Output
  16.     //  Tadpole  400 
  17.     //  Pinwheel  25 
  18.     //  Milky Way  0 
  19.     //  Andromeda  3 
  20.  
  21. public class Galaxy 
  22.     public string Name { get; set; } 
  23.     public int MegaLightYears { get; set; } 

 

責任編輯:武曉燕 來源: UP技術(shù)控
相關推薦

2021-11-09 12:11:55

C# Redis隊列

2022-04-26 09:01:39

實用工具類型TypeScript

2021-11-15 10:29:39

Go語言類型

2022-01-02 08:43:46

Python

2022-02-07 11:01:23

ZooKeeper

2023-01-03 08:31:54

Spring讀取器配置

2021-05-11 08:54:59

建造者模式設計

2021-07-05 22:11:38

MySQL體系架構(gòu)

2021-07-06 08:59:18

抽象工廠模式

2022-08-26 09:29:01

Kubernetes策略Master

2023-11-28 08:29:31

Rust內(nèi)存布局

2021-07-02 09:45:29

MySQL InnoDB數(shù)據(jù)

2022-08-23 08:00:59

磁盤性能網(wǎng)絡

2021-07-02 08:51:29

源碼參數(shù)Thread

2021-07-16 22:43:10

Go并發(fā)Golang

2021-09-28 08:59:30

復原IP地址

2022-04-12 08:30:52

回調(diào)函數(shù)代碼調(diào)試

2021-10-27 09:59:35

存儲

2022-03-11 10:21:30

IO系統(tǒng)日志

2023-03-13 21:38:08

TCP數(shù)據(jù)IP地址
點贊
收藏

51CTO技術(shù)棧公眾號