Console.WriteLine("***************Array******************");
int[] intArray = new int[3];
intArray[0] = 123;
string[] stringArray = new string[] { "123", "234" };//Array
Console.WriteLine("***************ArrayList******************");
ArrayList arrayList = new ArrayList();
arrayList.Add("Eleven");
arrayList.Add("Is");
arrayList.Add(32);//add增加長度
//arrayList[4] = 26;//索引覆制,不會增加長度
//刪除資料
arrayList.RemoveAt(4);
var value = arrayList[2];
arrayList.RemoveAt(0);
arrayList.Remove("Eleven");
Console.WriteLine("***************List<T>******************");
List<int> intList = new List<int>() { 1, 2, 3, 4 };
intList.Add(123);
intList.Add(123);
//intList.Add("123");
//intList[0] = 123;
Console.WriteLine("***************LinkedList<T>******************");
LinkedList<int> linkedList = new LinkedList<int>();
//linkedList[3] =>不能直接座標訪問
linkedList.AddFirst(123);
linkedList.AddLast(456);
bool isContain = linkedList.Contains(123);
LinkedListNode<int> node123 = linkedList.Find(123); //找元素123的位置 從頭開始找
linkedList.AddBefore(node123, 123);
linkedList.AddBefore(node123, 123);
linkedList.AddAfter(node123, 9);
//移除&清除
linkedList.Remove(456);
linkedList.Remove(node123);
linkedList.RemoveFirst();
linkedList.RemoveLast();
linkedList.Clear();
Console.WriteLine("***************Queue<T>******************");
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("four");
numbers.Enqueue("five");
//印出全部資料
foreach (string number in numbers)
{
Console.WriteLine(number);
}
//Dequeuing 會取出資料並從Queue中移除
Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'");
//Peek 會取出資料
Console.WriteLine($"Peek at next item to dequeue: { numbers.Peek()}");
Console.WriteLine($"Dequeuing '{numbers.Dequeue()}'");
//也可以一開始指定陣列的資料禁去
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
foreach (string number in queueCopy)
{
Console.WriteLine(number);
}
Console.WriteLine($"queueCopy.Contains(\"four\") = {queueCopy.Contains("four")}");
queueCopy.Clear();
Console.WriteLine($"queueCopy.Count = {queueCopy.Count}");
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");//放進去
foreach (string number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine($"Pop '{numbers.Pop()}'");//獲取並移除
Console.WriteLine($"Peek at next item to dequeue: { numbers.Peek()}");//獲取不移除
Console.WriteLine($"Pop '{numbers.Pop()}'");
Stack<string> stackCopy = new Stack<string>(numbers.ToArray());
foreach (string number in stackCopy)
{
Console.WriteLine(number);
}
Console.WriteLine($"stackCopy.Contains(\"four\") = {stackCopy.Contains("four")}");
stackCopy.Clear();
Console.WriteLine($"stackCopy.Count = {stackCopy.Count}");
Console.WriteLine("***************HashSet<string>******************");
HashSet<string> hashSet = new HashSet<string>();
hashSet.Add("123");
hashSet.Add("689");
hashSet.Add("456");
hashSet.Add("12435");
hashSet.Add("12435");
hashSet.Add("12435");
//hashSet[0];
foreach (var item in hashSet)
{
Console.WriteLine(item);
}
Console.WriteLine(hashSet.Count);
Console.WriteLine(hashSet.Contains("12345"));
HashSet<string> hashSet1 = new HashSet<string>();
hashSet1.Add("123");
hashSet1.Add("689");
hashSet1.Add("789");
hashSet1.Add("12435");
hashSet1.Add("12435");
hashSet1.Add("12435");
//求兩個集合的對稱差集。
hashSet1.SymmetricExceptWith(hashSet);
//求兩個集合的並集
hashSet1.UnionWith(hashSet);
//求兩個集合的差集。
hashSet1.ExceptWith(hashSet);
//求兩個集合的交集。
hashSet1.IntersectWith(hashSet);
hashSet.ToList();
hashSet.Clear();
Console.WriteLine("***************SortedSet<string>******************");
SortedSet<string> sortedSet = new SortedSet<string>();
sortedSet.Add("123");
sortedSet.Add("689");
sortedSet.Add("456");
sortedSet.Add("12435");
sortedSet.Add("12435");
sortedSet.Add("12435");
foreach (var item in sortedSet)
{
Console.WriteLine(item);
}
Console.WriteLine(sortedSet.Count);
Console.WriteLine(sortedSet.Contains("12345"));
SortedSet<string> sortedSet1 = new SortedSet<string>();
sortedSet1.Add("123");
sortedSet1.Add("689");
sortedSet1.Add("456");
sortedSet1.Add("12435");
sortedSet1.Add("12435");
sortedSet1.Add("12435");
//求兩個集合的對稱差集。
sortedSet1.SymmetricExceptWith(sortedSet);
//求兩個集合的並集
ortedSet1.UnionWith(sortedSet);
//求兩個集合的差集。
sortedSet1.ExceptWith(sortedSet);
//求兩個集合的交集。
sortedSet1.IntersectWith(sortedSet);
sortedSet.ToList();
sortedSet.Clear();
public class Box
{
/// <summary>
/// 高度
/// </summary>
public double Height { get; set; }
/// <summary>
/// 長度
/// </summary>
public double Length { get; set; }
/// <summary>
/// 寬度
/// </summary>
public double Width { get; set; }
}
//撰寫新的排序規則繼承IComparer<T>
public class BoxComp : IComparer<Box>
{
// Compares by Height, Length, and Width.
public int Compare(Box x, Box y)
{
if (x.Height.CompareTo(y.Height) != 0)
{
return x.Height.CompareTo(y.Height);
}
else if (x.Length.CompareTo(y.Length) != 0)
{
return x.Length.CompareTo(y.Length);
}
else if (x.Width.CompareTo(y.Width) != 0)
{
return x.Width.CompareTo(y.Width);
}
else
{
return 0;
}
}
}
}
//建立自定義的排序物件
var boxIComparer = new BoxComp();
//建立SortedSet將排序物件放進建構子當中
SortedSet<Box> boxSortedSet = new SortedSet<Box>(boxIComparer);
Console.WriteLine("***************Hashtable******************");
Hashtable table = new Hashtable();
table.Add("123", "456");
table[234] = 456;
table[234] = 567;
table[32] = 4562;
table[1] = 456;
table["eleven"] = 456;
foreach (DictionaryEntry objDE in table)
{
Console.WriteLine(objDE.Key.ToString());
Console.WriteLine(objDE.Value.ToString());
}
Hashtable.Synchronized(table);//可以保證 只有一個現成寫 多個現成讀
Console.WriteLine("***************Dictionary******************");
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
dic.Add(4, "HuHu1");
dic[4] = "HuHu";
dic.Add(4, "HuHu");
foreach (var item in dic)
{
Console.WriteLine($"Key:{item.Key}, Value:{item.Value}");
}
Console.WriteLine("***************SortedDictionary******************");
SortedDictionary<int, string> dic = new SortedDictionary<int, string>();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
dic.Add(4, "HuHu1");
dic[4] = "HuHu";
dic.Add(4, "HuHu");
foreach (var item in dic)
{
Console.WriteLine($"Key:{item.Key}, Value:{item.Value}");
}
Console.WriteLine("***************SortedList******************");
SortedList sortedList = new SortedList();//IComparer
sortedList.Add("First", "Hello");
sortedList.Add("Second", "World");
sortedList.Add("Third", "!");
sortedList["Third"] = "~~";//
sortedList.Add("Fourth", "!");
sortedList.Add("Fourth", "!");//重覆的Key Add會錯
sortedList["Fourth"] = "!!!";
var keyList = sortedList.GetKeyList();
var valueList = sortedList.GetValueList();
sortedList.TrimToSize();//用於最小化集合的內存開銷
sortedList.Remove("Third");
sortedList.RemoveAt(0);
sortedList.Clear();
ConcurrentQueue 線程安全的版本的Queue
ConcurrentStack 線程安全的版本的Stack
ConcurrentBag 線程安全的的對象集合
ConcurrentDictionary 線程安全的Dictionary
BlockingCollection
void Main()
{
foreach (var i in CreateEnumerable())
{
Console.WriteLine("外部迴圈讀取 => 傳回值【{0}】", i);
}
}
// Define other methods and classes here
public IEnumerable<int> CreateEnumerable()
{
Console.WriteLine("{0} CreateEnumerable()方法開始", DateTime.Now);
for (int i = 0; i < 5; i++)
{
Console.WriteLine("{0}開始 yield {1}", DateTime.Now, i);
yield return i;
Console.WriteLine("{0}yield 結束", DateTime.Now);
}
}
public class Food
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
/// <summary>
/// 肯德基的菜單
/// </summary>
public class KFCMenu
{
private Food[] _FoodList = new Food[3];
public KFCMenu()
{
this._FoodList[0] = new Food()
{
Id = 1,
Name = "漢堡包",
Price = 15
};
this._FoodList[1] = new Food()
{
Id = 2,
Name = "可樂",
Price = 10
};
this._FoodList[2] = new Food()
{
Id = 3,
Name = "薯條",
Price = 8
};
}
public Food[] GetFoods()
{
return this._FoodList;
}
public IIterator<Food> GetEnumerator()
{
return new KFCMenuIterator(this);
}
}
/// <summary>
/// 麥當勞菜單
/// </summary>
public class MacDonaldMenu
{
private List<Food> _FoodList = new List<Food>();
public MacDonaldMenu()
{
this._FoodList.Add(new Food()
{
Id = 1,
Name = "雞肉捲",
Price = 15
});
this._FoodList.Add(new Food()
{
Id = 2,
Name = "紅豆派",
Price = 10
});
this._FoodList.Add(new Food()
{
Id = 3,
Name = "薯條",
Price = 9
});
}
public List<Food> GetFoods()
{
return this._FoodList;
}
public IIterator<Food> GetEnumerator()
{
return new MacDonaldIterator(this);
}
}
//Array
Console.WriteLine("**************kfc Show*************");
KFCMenu kfcMenu = new KFCMenu();
Food[] foodCollection = kfcMenu.GetFoods();
for (int i = 0; i < foodCollection.Length; i++)
{
Console.WriteLine("KFC: Id={0} Name={1} Price={2}",foodCollection[i].Id, foodCollection[i].Name, foodCollection[i].Price);
}
//List
Console.WriteLine("**************MacDonald Show*************");
MacDonaldMenu macDonaldMenu = new MacDonaldMenu();
List<Food> foodCollection = macDonaldMenu.GetFoods();
for (int i = 0; i < foodCollection.Count(); i++)
{
Console.WriteLine("MacDonald: Id={0} Name={1} Price={2}", foodCollection[i].Id, foodCollection[i].Name, foodCollection[i].Price);
}
public interface IIterator<T>
{
/// <summary>
/// 當前的對象
/// </summary>
T Current { get; }
/// <summary>
/// 移動到下一個對象,是否存在
/// </summary>
/// <returns></returns>
bool MoveNext();
/// <summary>
/// 重制
/// </summary>
void Reset();
}
public class KFCMenuIterator : IIterator<Food>
{
private Food[] _FoodList = null;
public KFCMenuIterator(KFCMenu kfcMenu)
{
this._FoodList = kfcMenu.GetFoods();
}
private int _CurrentIndex = -1;
public Food Current
{
get
{
return this._FoodList[_CurrentIndex];
}
}
public bool MoveNext()
{
return this._FoodList.Length > ++this._CurrentIndex;
}
public void Reset()
{
this._CurrentIndex = -1;
}
}
public class MacDonaldIterator : IIterator<Food>
{
private List<Food> _FoodList = null;
public MacDonaldIterator(MacDonaldMenu macDonaldMenu)
{
this._FoodList = macDonaldMenu.GetFoods();
}
private int _CurrentIndex = -1;
public Food Current
{
get
{
return this._FoodList[_CurrentIndex];
}
}
public bool MoveNext()
{
return this._FoodList.Count > ++this._CurrentIndex;
}
public void Reset()
{
this._CurrentIndex = -1;
}
}
IIterator<Food> foodIterator = kfcMenu.GetEnumerator();// new KFCMenuIterator(kfcMenu);
while (foodIterator.MoveNext())
{
Food food = foodIterator.Current;
Console.WriteLine("KFC: Id={0} Name={1} Price={2}", food.Id, food.Name, food.Price);
}
IIterator<Food> foodIterator = macDonaldMenu.GetEnumerator();// new MacDonaldIterator(macDonaldMenu);
while (foodIterator.MoveNext())
{
Food food = foodIterator.Current;
Console.WriteLine("MacDonald: Id={0} Name={1} Price={2}", food.Id, food.Name, food.Price);
}