集合
自從 C# 新增 LINQ,集合的部分用途很多被取代。
但兩者應用場合不同,集合本質上不是資料的操作,而是資料的來源,如果要對同一批資料進行反覆的操作,使用集合物件比較有可讀性,或者要對某個資料體進行增刪的動作,使用集合物件比較有效率。
LINQ 其實就像進一步操作集合物件的語法工具,所以我們應該隨上下文的語意切換 LINQ 和集合,而不是只認定其中一種、否定另外一種🤗
ArrayList 和 Hashtable
在 C# 還不支援泛型時,.NET Framework 最主要的資料結構類別是 System.Collections 的 ArrayList 和 Hashtable。
最常用的就這兩個,其它還會用到的大概就 Queue 和 Stack 吧?
後來引進 LINQ 時,這些類別並未實作其功能,只在新的 System.Collections.Generic 類別實作,因此漸漸用 List 和 Dictionary 取而代之,ArrayList 和 Hashtable 有種走入歷史的感覺~
寫 C# 程式很難抗拒 LINQ 蜜糖般的誘惑,空有 ArrayList 和 Hashtable 資料結構,沒有 LINQ 接手各式演算法,就不像在寫 C# 程式了。
List
串列資料結構,適合讀取資料。
Capacity
Count
Add()
AddRange()
AsReadOnly()
BinarySearch()
Clear()
Contains()
ConvertAll()
CopyTo()
Exists()
Find()
FindAll()
FindIndex()
FindLast()
FindLastIndex()
ForEach()
GetEnumerator()
GetRange()
IndexOf()
Insert()
InsertRange()
IsFixedSize()
IsReadOnly()
IsSynchronized()
LastIndexOf()
Remove()
RemoveAll()
RemoveAt()
RemoveRange()
Reverse()
Sort()
SyncRoot()
ToArray()
TrimExcess()
TrueForAll()
LinkedList
雙向鏈結串列資料結構,適合寫入資料。
Count
First
Last
AddAfter()
AddBefore()
AddFirst()
AddLast()
Clear()
Contains()
CopyTo()
Find()
FindLast()
GetEnumerator()
GetObjectData()
OnDeserialization()
Remove()
RemoveFirst()
RemoveLast()
SortedSet
資料不重複的有序集合。
Comparer
Count
Max
Min
Add()
Clear()
Contains()
CopyTo()
CreateSetComparer()
ExceptWith()
GetEnumerator()
GetObjectData()
GetViewBetween()
IntersectWith()
IsProperSubsetOf()
IsProperSupersetOf()
IsSubsetOf()
IsSupersetOf()
OnDeserialization()
Overlaps()
Remove()
RemoveWhere()
Reverse()
SetEquals()
SymmetricExceptWith()
TryGetValue()
UnionWith()
HashSet
資料不重複的無序集合。
Comparer
Count
Add()
Clear()
Contains()
CopyTo()
CreateSetComparer()
EnsureCapacity()
ExceptWith()
GetEnumerator()
GetObjectData()
IntersectWith()
IsProperSubsetOf()
IsProperSupersetOf()
IsSubsetOf()
IsSupersetOf()
OnDeserialization()
Overlaps()
Remove()
RemoveWhere()
SetEquals()
SymmetricExceptWith()
TrimExcess()
TryGetValue()
UnionWith()
Queue
佇列,先進先出資料結構。
Count
Clear()
Contains()
CopyTo()
Dequeue()
Enqueue()
GetEnumerator()
Peek()
IsSynchronized()
SyncRoot()
ToArray()
TrimExcess()
TryDequeue()
TryPeek()
Stack
堆疊,後進先出資料結構。
Count
Clear()
Contains()
CopyTo()
GetEnumerator()
IsSynchronized()
Peek()
Pop()
Push()
SyncRoot()
ToArray()
TrimExcess()
TryPeek()
TryPop()
Dictionary
無序鍵值對資料結構。
Comparer
Count
Keys
Values
Add()
Clear()
ContainsKey()
Contains()
ContainsValue()
CopyTo()
EnsureCapacity()
GetEnumerator()
GetObjectData()
IsFixedSize()
IsReadOnly()
IsSynchronized()
OnDeserialization()
Remove()
SyncRoot()
TrimExcess()
TryAdd()
TryGetValue()
依值排序
這要用 LINQ 來完成:
ForEach()
這可以用 C# 的「擴充」來實現:
別忘了 LinqExtensions 可以單獨寫在一個 *.cs 檔案,C# 原本就能將類別分開寫的關係,Program 無須 using 就能載入使用。
SortedDictionary
無索引值的有序鍵值對資料結構。
Comparer
Count
Item[]
Keys
Values
Add()
Clear()
ContainsKey()
ContainsValue()
CopyTo()
GetEnumerator()
Remove()
TryGetValue()
SortedList
有索引值的有序鍵值對資料結構。
Capacity
Comparer
Count
Item[]
Keys
Values
Add()
Clear()
ContainsKey()
ContainsValue()
GetEnumerator()
IndexOfKey()
IndexOfValue()
Remove()
RemoveAt()
TrimExcess()
TryGetValue()
IEnumerable
IEnumerable.GetEnumerator()
IEnumerator
IEnumerator.Current
IEnumerator.Dispose()
IEnumerator.MoveNext()
IEnumerator.Reset()
語法糖
new List<型態>() {資料, 資料, ...};
new Dictionary<型態, 型態>() {{鍵, 值}, {鍵, 值}, ...};
打亂資料
做法一:用 LINQ 的 OrderBy()
做法二:為 IList 介面擴充 Shuffle()
做法三:打亂集合的 Sort()
讀寫檔案資料
使用 File 簡易讀寫
如果資料是 String 陣列或 List 串列,可以用 WriteAllLines 一次寫入新的檔案,用 AppendAllLines 一次附加資料到檔案,且每筆資料附加換行字元。
使用 StreamWriter 和 StreamReader 分批讀寫
使用 FileStream 隨機存取
使用 BinaryWriter 和 BinaryReader 讀寫數值資料