Programming C#, 3rd Edition Introduction (part 1) 寶來 資工部 簡伯松 PoSung Chien (Poisson)
Agenda <ul><li>Operator overloading 運算子的多載 </li></ul><ul><li>Structs  結構 </li></ul><ul><li>Streams 資料流 </li></ul>
CHAPTER 6 <ul><li>Operator overloading </li></ul>
運算子的分類 <ul><li>Arithmetic operators ( 算數運算 ) </li></ul><ul><li>Comparison operators / Relational operators  ( 比較運算 ) </li>...
Arithmetic operators ( 算數運算 ) Operator name Syntax Overloadable Basic assignment a  =  b No Addition a  +  b Yes Subtracti...
Comparison operators /  Relational operators ( 比較運算 ) Operator name Syntax Overloadable Equal to a  ==  b Yes Not equal to...
Logical operators ( 邏輯運算 ) Operator name Syntax Overloadable Logical negation (NOT) ! a Yes Logical AND a  &&  b Yes Logic...
Bitwise operators ( 位元運算 ) Operator name Syntax Overloadable Bitwise NOT ~ a Yes Bitwise AND a  &  b Yes Bitwise OR a  |  ...
Compound-assignment operators  ( 複合賦值 ) Operator name Syntax Overloadable Addition assignment a  +=  b Yes Subtraction ass...
Member and pointer operators  ( 成員及指標 ) Operator name Syntax Overloadable Array subscript a [ b ] Yes Indirection (&quot;v...
Other operators ( 其它 ) Operator name Syntax Overloadable Function call  See Function object . a ( a1, a2 ) Yes is operator...
Unsafe  <ul><li>unsafe  void Func ()  {   // Unsafe context: can use pointers here.  } </li></ul><ul><li>void  Func () { u...
運算子多載 <ul><li>使用者可自行定義運算子 </li></ul>Stocks A = new Stocks(); Stocks B = new Stocks(); A + B = ? int add = 100 + 200;
DEMO <ul><li>專案  PresentationTopic3  (operator_overloading)  VS 2005 </li></ul><ul><li>Arithmetic operators </li></ul><ul>...
Operator pair define <ul><li>當運算子  ==   定義時,編譯器會要求 運算子  !=   也必需被定義。  (error) </li></ul><ul><li>當運算子  ==   定義時,編譯器會暗示 需要覆寫...
運算子多載的缺點 <ul><li>混淆直覺 </li></ul><ul><ul><li>看似簡單的用法,其實耗時又複雜 </li></ul></ul><ul><li>容易誤用 </li></ul><ul><ul><li>數量多時容易交叉誤用 <...
CHAPTER 7 <ul><li>structs </li></ul>
Structs  <ul><li>輕量型的資料包裝 </li></ul><ul><li>與  Class   非常相似 </li></ul><ul><li>不能繼承  class ,  struct </li></ul><ul><li>不能定義...
Structs  定義 <ul><li>[attributes]  struct   identifier [:interfaces-list] { body } </li></ul>
DEMO <ul><li>專案  PresentationTopic3  (struct_default_constructor)  VS 2005 </li></ul><ul><li>專案  PresentationTopic3  (stru...
Creating structs without new <ul><li>原本  struct  abc =  new  struct(); </li></ul><ul><li>不用  new  的方法  struct  abc; </li><...
CHAPTER 21 <ul><li>streams </li></ul>
Agenda <ul><li>Working with Directories </li></ul><ul><li>Working with Files </li></ul><ul><li>Binary Files </li></ul><ul>...
Working with Directories <ul><li>Directory  class  成員均為  static </li></ul><ul><li>DirectoryInfo  class  成員均不為  static </li...
Directory class methods <ul><li>CreateDirectory() </li></ul><ul><li>GetCreationTime() SetCreationTime() </li></ul><ul><li>...
DirectoryInfo class methods <ul><li>Name </li></ul><ul><li>Parent </li></ul><ul><li>Root </li></ul><ul><li>Create() </li><...
Directory VS. DirectoryInfo <ul><li>Directory.CreateDirectory(path); </li></ul><ul><li>Directory.SetCreationTime(path, cre...
DEMO <ul><li>專案  PresentationTopic3_IO  (DirectoryAccess) VS 2005 </li></ul>
Working with Files <ul><li>File   class  成員均為  static </li></ul><ul><li>FileInfo   class  成員均不為  static </li></ul>
Experiment environment  <ul><li>針對同一檔案讀取  1000  次 </li></ul><ul><li>對  1000  個不同檔案各讀取 1 次 </li></ul><ul><li>比較  File  與  F...
Performance of reading File
Reading or Writing Class  Use  Stream   Abstract class that supports reading and writing bytes.  BinaryReader/ BinaryWrite...
Reading or Writing (cont.) Class  Use  BufferedStream   A stream that adds buffering to another stream such as a  NetworkS...
Binary Files <ul><li>BinaryReader  /  BinaryWriter   用在不知道檔案是否為單純的 文字檔 。 </li></ul>
Buffered Streams <ul><li>增加緩衝區,減少呼叫  System Call  的次數。 </li></ul><ul><li>如果總是存取大於內部緩衝區大小,  BufferedStream   可能 無法 配置緩衝區。 <...
實作 buffered I/O <ul><li>Stream  inputStream =  File .OpenRead(@“C:testfolder3.cs”); </li></ul><ul><li>Stream  outputStream...
實作 buffered I/O (cont.) <ul><li>while ( (bytesRead = bufferedInput. Read ( buffer,offset,SIZE_BUFF)) > 0 )  { //  讀取後寫入   ...
Working with Text Files <ul><li>讀取文字檔可使用  StreamReader   和 StreamWriter   較為方便容易。 </li></ul>
DEMO <ul><li>專案  PresentationTopic3_IO  (BinaryStream)  VS 2005 </li></ul>
Thanks for attention
Upcoming SlideShare
Loading in …5
×

Programming c sharp 3rd ch6 7 21

806 views
708 views

Published on

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

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
806
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Reference : http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : 不可多載的運算子 in c# http://msdn.microsoft.com/zh-tw/library/8edha89s(VS.80).aspx
  • Reference : http://msdn.microsoft.com/zh-tw/library/7bxsh36e%28v=VS.90%29.aspx 編譯器錯誤 CS0216 Reference : http://msdn.microsoft.com/zh-tw/library/4wtxwb6k%28VS.80%29.aspx 編譯器警告 ( 層級 3) CS0660
  • Reference : http://lagunawang.pixnet.net/blog/post/20160001
  • Reference : http://msdn.microsoft.com/zh-tw/library/saxz13w4%28v=vs.80%29.aspx The stack is always used to store the following two things: The reference portion of reference-typed local variables and parameters (such as the myTextBox reference ) Value-typed local variables and method parameters (structs, as well as integer s, bool s, char s, DateTime s, etc.) The following data is stored on the heap: The content of reference-type objects. Anything structured inside a reference-type object. 只要 struct 是放在 class 裡 這個 struct 也是會被放在 Heap 而不是 Stack. struct 裡面有 class 的話, struct 會存在 Stack 裡,而其中有一個 指標指到 Heap 中 class 。 Class 裡面有 struct 的話, stack 裡只會在 class 的指標,而 struct 會存在 Heap 中的 Class 裡
  • Reference : http://msdn.microsoft.com/zh-tw/library/ah19swz4%28v=vs.80%29.aspx Reference : http://msdn.microsoft.com/en-us/library/ah19swz4%28v=vs.71%29.aspx [attributes] : public, private
  • Reference : http://stackoverflow.com/questions/3146586/directory-vs-directoryinfo
  • Reference : http://msdn.microsoft.com/zh-tw/library/system.io.directoryinfo_methods%28v=VS.80%29.aspx public static IEnumerable&lt;string&gt; EnumerateDirectories( string path ) public IEnumerable&lt;FileInfo&gt; EnumerateFiles() public FileSystemInfo[] GetFileSystemInfos ( string searchPattern ) public static string[] GetDirectories ( string path )
  • 先用 stream 打開檔案,再用 BufferedStream 來承接, 之後我們就對 BufferedStream 做操作既可。
  • Programming c sharp 3rd ch6 7 21

    1. 1. Programming C#, 3rd Edition Introduction (part 1) 寶來 資工部 簡伯松 PoSung Chien (Poisson)
    2. 2. Agenda <ul><li>Operator overloading 運算子的多載 </li></ul><ul><li>Structs 結構 </li></ul><ul><li>Streams 資料流 </li></ul>
    3. 3. CHAPTER 6 <ul><li>Operator overloading </li></ul>
    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
    32. 32. Reading or Writing Class Use Stream Abstract class that supports reading and writing bytes. BinaryReader/ BinaryWriter Read and write encoded strings and primitive datatypes to and from streams. File , FileInfo , Directory , DirectoryInfo Provide implementations for the abstract FileSystemInfo classes, including creating, moving, renaming, and deleting files and directories. FileStream For reading to and from File objects; supports random access to files. Opens files synchronously by default; supports asynchronous file access. TextReader , T ex tWriter , StringReader , S t ringWriter TextReader and TextWriter are abstract classes designed for Unicode character I/O. StringReader and StringWriter write to and from strings, allowing your input and output to be either a stream or a string.
    33. 33. Reading or Writing (cont.) Class Use BufferedStream A stream that adds buffering to another stream such as a NetworkStream . Note that FileStream has buffering built in. BufferedStreams can improve performance of the stream to which they are attached. MemoryStream A nonbuffered stream whose encapsulated data is directly accessible in memory. A MemoryStream has no backing store, and is most useful as a temporary buffer. NetworkStream A stream over a network connection.
    34. 34. Binary Files <ul><li>BinaryReader / BinaryWriter 用在不知道檔案是否為單純的 文字檔 。 </li></ul>
    35. 35. Buffered Streams <ul><li>增加緩衝區,減少呼叫 System Call 的次數。 </li></ul><ul><li>如果總是存取大於內部緩衝區大小, BufferedStream 可能 無法 配置緩衝區。 </li></ul><ul><li>BufferedStream must be initialized by some other existing Stream . </li></ul><ul><li>一般檔案存取已經是使用緩衝區,如 StreamReader / StreamWriter . </li></ul>
    36. 36. 實作 buffered I/O <ul><li>Stream inputStream = File .OpenRead(@“C:testfolder3.cs”); </li></ul><ul><li>Stream outputStream = File .OpenWrite(@&quot;C:testfolder3.bak&quot;); </li></ul><ul><li>BufferedStream bufferedInput = new BufferedStream (inputStream); </li></ul><ul><li>BufferedStream bufferedOutput = new BufferedStream (outputStream); </li></ul>
    37. 37. 實作 buffered I/O (cont.) <ul><li>while ( (bytesRead = bufferedInput. Read ( buffer,offset,SIZE_BUFF)) > 0 ) { // 讀取後寫入 bufferedOutput. Write ( buffer,offset,bytesRead); } </li></ul><ul><li>bufferedOutput. Flush ( ); </li></ul><ul><li>bufferedInput. Close ( ); </li></ul><ul><li>bufferedOutput. Close ( ); </li></ul>
    38. 38. Working with Text Files <ul><li>讀取文字檔可使用 StreamReader 和 StreamWriter 較為方便容易。 </li></ul>
    39. 39. DEMO <ul><li>專案 PresentationTopic3_IO (BinaryStream) VS 2005 </li></ul>
    40. 40. Thanks for attention

    ×