Day6
一、Guid
1、Getting the string representation of a Guid——获取Guid的字符串表示形式
可以使用内置的ToString方法获得Guid的字符串表示:
string myGuidString = myGuid.ToString();
在ToString调用中添加格式化类型参数来格式化Guid
N:去掉-
D:保持不变
B:加{ }
P:加上()
X:加上{ },并且用16进制表示

2、创建Guid
创建一个guid(00000000000000000000000000000000000000):
1 Guid g = Guid.Empty; 2 Guid g2 = new Guid();
创建一个伪随机的guid
Guid g = Guid.NewGuid();
创建具有特定值的向导:
1 Guid g = new Guid("0b214de7-8958-4956-8eed-28f9ba2c47c6"); 2 Guid g2 = new Guid("0b214de7895849568eed28f9ba2c47c6"); 3 Guid g3 = Guid.Parse("0b214de7-8958-4956-8eed-28f9ba2c47c6");
3、Declaring a nullable GUID——声明一个可为空的GUID
Guid? myGuidVar = null;
二、BigInteger
1、计算前1000位斐波那契数

三、初始化
1、例如初始化一个数组
var stringList = new List<string> { "foo", "bar", };
等同于:
1 var temp = new List<string>(); 2 temp.Add("foo"); 3 temp.Add("bar"); 4 var stringList = temp;
2、C# 6 Index Initializers——C# 6 索引初始化器
例如在字典里面的用法是
1 var dict = new Dictionary<string, int> 2 { 3 ["key1"] = 1, 4 ["key2"] = 50 5 };
相当于:
1 var dict = new Dictionary<string, int>(); 2 dict["key1"] = 1; 3 dict["key2"] = 50
等于:
1 var dict = new Dictionary<string, int>(); 2 dict.Add("key1", 1); 3 dict.Add("key2", 50);
此方法节省了add
1 public class IndexableClass 2 { 3 public int this[int index] 4 { 5 set 6 { 7 Console.WriteLine("{0} was assigned to index {1}", value, index); 8 } 9 } 10 } 11 var foo = new IndexableClass 12 { 13 [0] = 10, 14 [1] = 20 15 }
结果是:
10 was assigned to index 0
20 was assigned to index 1
四、An overview of C# collections——c#集合的概述
1、HashSet<T>
Hashset 存储的数据是无序并且唯一的,底层使用HashMap存储数据,值存在于hashmap的key中,value都是内部定义的一个PRESENT.(HashSet的底层数据结构是hashMap)时间复杂度O(1)
List存储的数据是有序并且可以重复的。 采用数组或者链表存储数据。如Arraylist就是数组存储数据.(List的底层数据结构是数组或链表)时间复杂度O(n)
2、Dictionary<TKey, TValue>

3、SortedSet<T>

SortedSet<T>升序排列。唯一
4、T[ ] (Array of T)

5、List<T>
1 var list = new List<int>() { 1, 2, 3, 4, 5 }; 2 list.Add(6); 3 Console.WriteLine(list.Count); //输出 6 4 list.RemoveAt(3); 5 Console.WriteLine(list.Count); //输出 5 6 Console.WriteLine(list[3]); // 输出 5
6、Stack<T>
var stack = new Stack<int>(); stack.Push(3); stack.Push(5); stack.Push(8); Console.WriteLine(stack.Peek()); // 输出 8 Console.WriteLine(stack.Pop()); // 输出 8 Console.WriteLine(stack.Pop()); // 输出 5 Console.WriteLine(stack.Pop()); // 输出 3
7、 LinkedList<T>
1 LinkedList list = new LinkedList<int>(); 2 list.AddLast(3); 3 list.AddLast(5); 4 list.AddLast(8); 5 // list里面现在是 3, 5, 8 6 list.AddFirst(2); 7 // list里面现在是 2, 3, 5, 8 8 list.RemoveFirst(); 9 // list里面现在是 3, 5, 8 10 list.RemoveLast(); 11 // list里面现在是 3, 5
8、Queue
1 var queue = new Queue<int>(); 2 queue.Enqueue(6); 3 queue.Enqueue(4); 4 queue.Enqueue(9); 5 // 队列现在是: 6, 4, 9 6 Console.WriteLine(queue.Peek()); // 输出 6 7 Console.WriteLine(queue.Dequeue()); // 输出 6 8 Console.WriteLine(queue.Dequeue()); // 输出 4 9 Console.WriteLine(queue.Dequeue()); // 输出 9

浙公网安备 33010602011771号