Using Common Reference Types
String and String Builder
System.String are immutable in .NET. That means any change to a string causes the runtime to create a new string and abandon the old one.
StringBuilder class to create dynamic (mutable) strings. The StringBuilder solution is the most flexible because it can span multiple statements. The default constructor creates a buffer 16 bytes long, which grows as needed. You can specify an initial size and a maximum size if you like.
Throw and Catch Exceptions
Most types in the .NET Framework are reference types. Reference types provide a great deal of flexibility, and they offer excellent performance when passing them to methods.
Reference types store the address of their data, also known as a pointer, on the stack. The actual data that address refers to is stored in an area of memory called the heap.
The runtime manages the memory used by the heap through a process called garbage collection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced.
Reference types represent the address of data rather than the data itself, assigning one reference variable to another doesn't copy the data. Instead, assigning a reference variable to another instance merely creates a second copy of the reference, which refers to the same memory location on the heap as the original variable.
Type | Use for |
---|---|
System.Object | The Object type is the most general type in the Framework. You can convert any type to System.Object, and you can rely on any type having ToString, GetType, and Equals members inherited from this type. |
System.String | Text data. |
System.Text.StringBuilder | Dynamic text data. |
System.Array | Arrays of data. This is the base class for all arrays. Array declarations use language-specific array syntax. |
System.IO.Stream | Buffer for file, device, and network I/O. This is an abstract base class; task-specific classes are derived from Stream. |
System.Exception | Handling system and application-defined exceptions. Task-specific exceptions inherit from this type. |
String and String Builder
System.String are immutable in .NET. That means any change to a string causes the runtime to create a new string and abandon the old one.
StringBuilder class to create dynamic (mutable) strings. The StringBuilder solution is the most flexible because it can span multiple statements. The default constructor creates a buffer 16 bytes long, which grows as needed. You can specify an initial size and a maximum size if you like.
Throw and Catch Exceptions
Exceptions are unexpected events that interrupt normal execution of an assembly. For example, if your assembly is reading a large text file from a removable disk and the user removes the disk, the runtime will throw an exception. This makes sense because there is no way your assembly could continue running.
Exceptions should never cause your assembly to fail completely. Instead, you should plan for exceptions to occur, catch them, and respond to the event. In the preceding example, you could notify the user that the file was not available, and then await further instructions from the user.
Syntax for Try Catch
' VB Try ' coding
Catch ex As Exception ' If there are any problems reading the file, display an error message
Finally
' Finally block
End Try
In the preceding example, if any type of error occurs—including a file not found error, insufficient privileges error, or an error during the reading of the file—processing continues within the Catch block. If no problems occur, the runtime skips the Catch block.
Note:
The runtime will execute only the first Catch block with a matching exception type, however, so order Catch blocks from the most-specific to the least-specific.
The Finally block runs after the Try block and any Catch blocks have finished executing, whether or not an exception was thrown. Therefore, you should use a Finally block to close any streams or clean up any other objects that might be left open if an exception occurs.
No comments:
Post a Comment