MCTS Examen 70-536 – Common Reference Types
Hello everyone !
Hallo zusammen,
ein neuer Anlauf in Richtung Examen und die Entscheidung, die Blogsprache auf Englisch umzustellen, um schlichtweg mehr Leser anzusprechen. Ich hoffe, es stört nicht zu sehr
)
During todays power-cut (don’t know what happened, just no electricity for half an hour) I decided to continue my journey toward Microsoft exam 70-735, the first step to become MCPD.
I will post a summary of any chapter I work through, therefore its not exactly for beginners, more like a little learning help for me and hopefully other people reading it. By the way, my language of choice is C#, so this is what you will read about.
Summary about common reference types
In General
- Everything that not derives from System.ValueType is a Referencetype.
- Some build-In Reference types (There are about 2.500 in total): System.Object, System.String, System.Text.StringBuilder, System..Array, System.IO.Stream, System.Exception and also Structures we define.
- Data itself is stored on the heap, a reference (pointer) to it is stored on the stack where all the value types are.
- The Garbage Collector can delete the data on the heap, once there is no strong reference to it on the stack anymore.
- Assigning a Reference Variable to another one doesn’t copy the data, it copies the reference.
About System.String
- Strings are Immutable. When you change it, there will be a new Object created and the old one will be abandoned.
- Avoid String-Operations except Join, Concat and Format or use a StringBuilder instead.
- Strings inherit Operators like + (Join), == (Equals), != (Not Equal) and = (Assignment) from System.Object
About Arrays
- Example Declaration: int[] myarray = { 1, 2, 3 };
- Sorting can be done easy by using Array.Sort(myarray);
About Streams (System.IO.Stream)
- the main purpose of streams is to read from and write to disc and to communicate within the network.
- Networkstreams are in System.Network.Sockets and encrypted streams can be found in System.Security.Cryptography
- Read textfiles with StreamReader and pass the filename to the constructor.
- Write textfiles with StreamWriter and pass the filename to the constructor.
- Dont forget to close a stream by Close() when you’re done using it.
Exceptions – Throwing and Catching
- Exceptions are unexpected events. Normally because an operation failed.
- Don’t let Exceptions cause you’re Application to crash. Plan carefully instead.
- Put your code within a try-statement like this: try { code… }
- Let a catch-statement follow which handles the exception.: catch { handling… }
- The last part is an optional finally-statement followed by a block of code which will
be execute if or if not an exception occures. For example, to close a stream, even if an exception occured. - You can write different Catch-Statements for any type of exception occuring. for eachmple will Catch(FileNotFoundException) only be executed if a FileNotFoundException occures. Catch without any specification is the default handler, if no more specific handling is defined. therefor it should be the last handler you define in your code-block.
- Order Catch-Blocks from more specific to less specific. Sometimes called filtering exceptions.
- If you want to work with the errormessage, stacktrace or other informations in your catch-block, you can hand it over to a local variable you define by Catch(FileNotFoundException fnfex) for example. Now you can access this information in your Catch-Block like fnfex.Message etc.
- You can define your own Exception-Types which must inherit from System.Exception. You can catch them specifically
- If an exception is not handled, it will be passed to the method that called the current one and can still be handled there but does not always make sense.
- Strive for a good and robust error-handling within your application.
- Error-handling produces a slight performance penalty, but should be used anyway to make debugging easier and give the user a better experience.
Have I learned anything new ?
No, not really. A bit of brush-up, but nothing new to learn this time. But stay tuned
Sascha


