偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一篇學(xué)會(huì) C# 集合類型

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

[[439433]]

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

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

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

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

如果集合中只包含一種數(shù)據(jù)類型的元素,則可以使用 System.Collections.Generic 命名空間中的一個(gè)類。泛型集合強(qiáng)制類型安全,因此無法向其添加任何其他數(shù)據(jù)類型。當(dāng)你從泛型集合檢索元素時(shí),你無需確定其數(shù)據(jù)類型或?qū)ζ溥M(jìn)行轉(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)容是事先已知的,則可以使用集合初始值設(shè)定項(xiàng)來初始化集合。

  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)訪問集合。通過按索引位置訪問集合元素實(shí)現(xiàn)此目的。元素的索引開始于 0,結(jié)束于元素計(jì)數(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; } 

 

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

2021-11-09 12:11:55

C# Redis隊(duì)列

2022-01-02 08:43:46

Python

2022-02-07 11:01:23

ZooKeeper

2022-04-26 09:01:39

實(shí)用工具類型TypeScript

2021-11-15 10:29:39

Go語言類型

2021-07-02 09:45:29

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

2021-07-06 08:59:18

抽象工廠模式

2023-01-03 08:31:54

Spring讀取器配置

2021-07-05 22:11:38

MySQL體系架構(gòu)

2023-11-28 08:29:31

Rust內(nèi)存布局

2022-08-26 09:29:01

Kubernetes策略Master

2021-05-11 08:54:59

建造者模式設(shè)計(jì)

2022-08-23 08:00:59

磁盤性能網(wǎng)絡(luò)

2021-09-28 08:59:30

復(fù)原IP地址

2021-10-14 10:22:19

逃逸JVM性能

2022-04-12 08:30:52

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

2021-10-27 09:59:35

存儲(chǔ)

2021-07-16 22:43:10

Go并發(fā)Golang

2023-03-13 21:38:08

TCP數(shù)據(jù)IP地址

2023-11-01 09:07:01

Spring裝配源碼
點(diǎn)贊
收藏

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