Successfully reported this slideshow.
Upcoming SlideShare
×

# Programming c sharp 3rd ch6 7 21

843 views

Published on

publisher O'Reilly,
Programming C#, 3rd Edition,

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Programming c sharp 3rd ch6 7 21

1. 1. Programming C#, 3rd Edition Introduction (part 1) 寶來 資工部 簡伯松 PoSung Chien (Poisson)
4. 4. 運算子的分類 <ul><li>Arithmetic operators ( 算數運算 ) </li></ul><ul><li>Comparison operators / Relational operators ( 比較運算 ) </li></ul><ul><li>Logical operators ( 邏輯運算 ) </li></ul><ul><li>Bitwise operators ( 位元運算 ) </li></ul><ul><li>Compound-assignment operators ( 複合賦值 ) </li></ul><ul><li>Member and pointer operators ( 成員及指標 ) </li></ul><ul><li>Other operators ( 其它 ) </li></ul>
5. 5. Arithmetic operators ( 算數運算 ) Operator name Syntax Overloadable Basic assignment a = b No Addition a + b Yes Subtraction a - b Yes Unary plus (integer promotion) + a Yes Unary minus (additive inverse) - a Yes Multiplication a * b Yes Division a / b Yes Modulo (remainder) a % b Yes Increment ++ a, a ++ Yes Decrement -- a, a -- Yes
6. 6. Comparison operators / Relational operators ( 比較運算 ) Operator name Syntax Overloadable Equal to a == b Yes Not equal to a != b Yes Greater than a > b Yes Less than a < b Yes Greater than or equal to a >= b Yes Less than or equal to a <= b Yes
7. 7. Logical operators ( 邏輯運算 ) Operator name Syntax Overloadable Logical negation (NOT) ! a Yes Logical AND a && b Yes Logical OR a || b Yes
8. 8. Bitwise operators ( 位元運算 ) Operator name Syntax Overloadable Bitwise NOT ~ a Yes Bitwise AND a & b Yes Bitwise OR a | b Yes Bitwise XOR a ^ b Yes Bitwise left shift a << b Yes Bitwise right shift a >> b Yes
9. 9. Compound-assignment operators ( 複合賦值 ) Operator name Syntax Overloadable Addition assignment a += b Yes Subtraction assignment a -= b Yes Multiplication assignment a *= b Yes Division assignment a /= b Yes Modulo assignment a %= b Yes Bitwise AND assignment a &= b Yes Bitwise OR assignment a |= b Yes Bitwise XOR assignment a ^= b Yes Bitwise left shift assignment a <<= b Yes Bitwise right shift assignment a >>= b Yes
10. 10. Member and pointer operators ( 成員及指標 ) Operator name Syntax Overloadable Array subscript a [ b ] Yes Indirection (&quot;variable pointed to by a &quot;) * a No Reference (&quot;address of a &quot;) & a No Member b of object pointed to by a (unsafe) a -> b No Member b of object a a . b No Member pointed to by b of object pointed to by a (unsafe) a ->* b No Member pointed to by b of object a (unsafe) a .* b No
11. 11. Other operators ( 其它 ) Operator name Syntax Overloadable Function call See Function object . a ( a1, a2 ) Yes is operator object is string No True / False true, false Yes Ternary conditional a ? b : c No Size-of sizeof (a) sizeof ( type ) No Type identification typeof (a) typeof ( type ) No Cast (implicit, explicit) ( type ) a Yes Allocate storage new type No
12. 12. Unsafe <ul><li>unsafe void Func () { // Unsafe context: can use pointers here. } </li></ul><ul><li>void Func () { unsafe { // Unsafe context: can use pointers here. } } </li></ul>
13. 13. 運算子多載 <ul><li>使用者可自行定義運算子 </li></ul>Stocks A = new Stocks(); Stocks B = new Stocks(); A + B = ? int add = 100 + 200;
14. 14. DEMO <ul><li>專案 PresentationTopic3 (operator_overloading) VS 2005 </li></ul><ul><li>Arithmetic operators </li></ul><ul><li>Comparison operators </li></ul><ul><li>Conversion Operators </li></ul>
15. 15. Operator pair define <ul><li>當運算子 == 定義時，編譯器會要求 運算子 != 也必需被定義。 (error) </li></ul><ul><li>當運算子 == 定義時，編譯器會暗示 需要覆寫 Equals 函式。 (warning) </li></ul>
16. 16. 運算子多載的缺點 <ul><li>混淆直覺 </li></ul><ul><ul><li>看似簡單的用法，其實耗時又複雜 </li></ul></ul><ul><li>容易誤用 </li></ul><ul><ul><li>數量多時容易交叉誤用 </li></ul></ul>
17. 17. CHAPTER 7 <ul><li>structs </li></ul>
18. 18. Structs <ul><li>輕量型的資料包裝 </li></ul><ul><li>與 Class 非常相似 </li></ul><ul><li>不能繼承 class , struct </li></ul><ul><li>不能定義解構 deconstructor </li></ul><ul><li>不能定義 default constructor </li></ul><ul><li>不能宣告 field 時就給值 </li></ul><ul><li>成員不能宣告為 protected </li></ul><ul><li>可以繼承實作 interface </li></ul><ul><li>Class 是 Reference type 而 Struct 是 Value type </li></ul>
19. 19. Structs 定義 <ul><li>[attributes] struct identifier [:interfaces-list] { body } </li></ul>
20. 20. DEMO <ul><li>專案 PresentationTopic3 (struct_default_constructor) VS 2005 </li></ul><ul><li>專案 PresentationTopic3 (struct_distructor) VS 2005 </li></ul><ul><li>專案 PresentationTopic3 (struct_inheritance) VS 2005 </li></ul><ul><li>專案 PresentationTopic3 (struct_interface) VS 2005 </li></ul><ul><li>專案 PresentationTopic3 (struct_nonew) VS 2005 </li></ul>
21. 21. Creating structs without new <ul><li>原本 struct abc = new struct(); </li></ul><ul><li>不用 new 的方法 struct abc; </li></ul>
22. 22. CHAPTER 21 <ul><li>streams </li></ul>
23. 23. Agenda <ul><li>Working with Directories </li></ul><ul><li>Working with Files </li></ul><ul><li>Binary Files </li></ul><ul><li>Buffered Streams </li></ul><ul><li>Working with Text Files </li></ul><ul><li>Next presentation </li></ul><ul><ul><li>Serialization </li></ul></ul><ul><ul><li>Asynchronous I/O </li></ul></ul><ul><ul><li>Network I/O </li></ul></ul><ul><ul><li>Web Streams </li></ul></ul><ul><ul><li>Isolated Storage </li></ul></ul>
24. 24. Working with Directories <ul><li>Directory class 成員均為 static </li></ul><ul><li>DirectoryInfo class 成員均不為 static </li></ul><ul><li>針對同一目錄多次操作， DirectoryInfo 會有較好的效能。因為 DirectoryInfo 並不會每次的操作都做 security privileges check ( 權限檢查 ) 。 </li></ul><ul><li>Namespace </li></ul><ul><ul><li>Using System.IO ; </li></ul></ul>
25. 25. Directory class methods <ul><li>CreateDirectory() </li></ul><ul><li>GetCreationTime() SetCreationTime() </li></ul><ul><li>GetLastAccessTime() SetLastAccessTime() </li></ul><ul><li>GetLastWriteTime() SetLastWriteTime() </li></ul><ul><li>GetLogicalDrives() </li></ul><ul><li>GetParent() </li></ul><ul><li>GetDirectoryRoot() </li></ul><ul><li>GetFiles() </li></ul><ul><li>Move() </li></ul><ul><li>Exists </li></ul>
26. 26. DirectoryInfo class methods <ul><li>Name </li></ul><ul><li>Parent </li></ul><ul><li>Root </li></ul><ul><li>Create() </li></ul><ul><li>CreateSubdirectory() </li></ul><ul><li>Delete() </li></ul><ul><li>EnumerateDirectories() </li></ul><ul><li>EnumerateFiles() </li></ul><ul><li>GetDirectories() </li></ul><ul><li>GetFiles() </li></ul><ul><li>GetFileSystemInfos() </li></ul><ul><li>MoveTo() </li></ul><ul><li>Refresh() </li></ul>
27. 27. Directory VS. DirectoryInfo <ul><li>Directory.CreateDirectory(path); </li></ul><ul><li>Directory.SetCreationTime(path, createTime); </li></ul><ul><li>Directory.SetLastAccessTime(path, lastAccessTime); </li></ul><ul><li>Directory.SetLastWriteTime(path, lastWriteTime); </li></ul><ul><li>Directory.GetLogicalDrives() </li></ul><ul><li>Directory.GetParent(path) </li></ul><ul><li>Directory.GetDirectoryRoot(path) </li></ul><ul><li>Directory.GetFiles(path) </li></ul><ul><li>Directory.Move(path, newPath); </li></ul><ul><li>Directory.Exists(path) </li></ul><ul><li>DirectoryInfo directoryInfo = new DirectoryInfo(path); </li></ul><ul><li>directoryInfo.Create(); </li></ul><ul><li>directoryInfo.CreationTime = createTime; </li></ul><ul><li>directoryInfo.LastAccessTime = lastAccessTime; </li></ul><ul><li>directoryInfo.LastWriteTime = lastWriteTime; </li></ul><ul><li>directoryInfo.Parent </li></ul><ul><li>directoryInfo.Root </li></ul><ul><li>directoryInfo.GetFiles() </li></ul><ul><li>directoryInfo.MoveTo(newPath); </li></ul><ul><li>directoryInfo.Exists </li></ul>
28. 28. DEMO <ul><li>專案 PresentationTopic3_IO (DirectoryAccess) VS 2005 </li></ul>
29. 29. Working with Files <ul><li>File class 成員均為 static </li></ul><ul><li>FileInfo class 成員均不為 static </li></ul>
30. 30. Experiment environment <ul><li>針對同一檔案讀取 1000 次 </li></ul><ul><li>對 1000 個不同檔案各讀取 1 次 </li></ul><ul><li>比較 File 與 FileInfo 所花總時間差異 </li></ul><ul><li>專案 PresentationTopic3_IO (DataSetsGenerator) </li></ul><ul><li>專案 PresentationTopic3_IO (FileTest) </li></ul>
31. 31. Performance of reading File