Monday, 3 September 2007

lesson 2: Using Common Ref Types.

Ref Types Vs Value Types

  • Ref variable - holds the address of data rather than the data itself.
  • Assigning one ref. var to another - doesn't copy the data - merely creates 2nd copy of ref to same data.
  • 2500 built-in ref types - everything NOT derived from System.ValueType

Strings/StringBuilder

  • Immutable - cannot change - System.String.
  • Avoid temporary strings - s = "a"; s+= "qwerty"; - 2 strings created, of which only the last will have a ref. 1st garbage collected. Avoid unnecessary garbage collection here by using String class's Concat(), Join() & Format() mthds. Or use StringBuilder class to create mutable strings.
  • StringBuilder class - can span multiple stmts.

Create/Sort Arrays

  • int[] ar = {3,1,2};
  • Sort using static mthd - Array.Sort(ar);

Streams (System.IO.Stream)

  • common type - means of reading from/writing to disk.
  • StreamReader/StreamWriter - simplest - enable reading from/writing to txt files.
  • sr.ReadToEnd();

Exceptions

  • Catch block - skipped if no error encountered in try block.
  • define own exceptions - derive from System.ApplicationException.
  • Filtering Exceptions - mutiple catch blocks - runtime will only execute the 1st Catch block with a matching exception. THUS ORDER CATCH BLOCKS FROM MOST-SPECIFIC TO LEAST SPECIFIC.
  • Finally{} - runs whether an exception occured or not.
  • All code in try/catch blocks.

No comments: