Friday, March 11, 2011

Application Development Foundation - Part 3

Using Common Reference Types

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.

Common Reference Types
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.

Thursday, March 10, 2011

Application Development Foundation - Part 2

Using Value Types

  • The simplest types in the .NET Framework, primarily numeric and Boolean types, are value types.
  • Value types are variables that contain their data directly instead of containing a reference to the data stored elsewhere in memory.
  • Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update, and remove them quickly with minimal overhead.
There are three general value types:
  • Built-in types
  • User-defined types
  • Enumerations
Built-in types

Type (Visual Basic/C# alias)
Bytes
Range
Use for
System.SByte (SByte/sbyte)
1
-128 to 127
Signed byte values
System.Byte (Byte/byte)
1
0 to 255
Unsigned bytes
System.Int16 (Short/short)
2
-32768 to 32767
Interoperation and other specialized uses
System.Int32 (Integer/int)
4
-2147483648 to 2147483647
Whole numbers and counters
System.UInt32 (UInteger/uint)
4
0 to 4294967295
Positive whole numbers and counters
System.Int64 (Long/long)
8
-9223372036854775808 to 9223372036854775807
Large whole numbers
System.Single (Single/float)
4
-3.402823E+38 to 3.402823E+38
Floating point numbers
System.Double (Double/double)
8
-1.79769313486232E+308 to 1.79769313486232E+308
Precise or large floating point numbers
System.Decimal (Decimal/decimal)
16
-79228162514264337593543950335 to 79228162514264337593543950335
Financial and scientific calculations requiring great precision
System.Char (Char/char)
2
N/A
Single Unicode characters
System.Boolean (Boolean/bool)
4
N/A
True/False values
System.IntPtr (none)
Platform-dependent
N/A
Pointer to a memory address
System.DateTime (Date/date)
8
1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM
Moments in time
Note:
When you assign between value-type variables, the data is copied from one variable to the other and stored in two different locations on the stack.

User defined type

User-defined types are also called structures or simply structs, after the language keyword used to create them. As with other value types, instances of user-defined types are stored on the stack and they contain their data directly. In most other ways, structures behave nearly identical to classes.

[vbnet]
Structure <struct-name>
      'Body of structure
End Structure

[csharp]
public struct <struct-name>
{
      //Body of strucrue
}
Structures are a composite of other types that make it easier to work with related data. The simplest example of this is System.Drawing.Point, which contains X and Y integer properties that define the horizontal and vertical coordinates of a point.

The structure can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.

There is no inheritance for structs as there is for classes. A struct can't inherit from another struct or class and it can't be the base class for a class. But remember that in .NET all types are directly or indirectly inheriting from the super base class object and hence the structure also. Since structs doesn't support inheritance, we can't use the keywords virtual, override, new, abstract etc with a struct methods. .NET struct types are never abstract and are always implicitly sealed. The abstract or sealed modifiers are not permitted in a struct declaration. 

Enumerations
Enumerations are related symbols that have fixed values. Use enumerations to provide a list of choices for developers using your class. For example, the following enumeration contains a set of titles:
' VB
Enum Titles As Integer
    Mr
    Ms
    Mrs
    Dr
End Enum

// C#
enum Titles : int { Mr, Ms, Mrs, Dr };

Application Development Foundation - Part 1

Introduction

By using this, you'll learn how to do the following:
  • Develop applications that use system types and collections.
  • Implement service processes, threading, and application domains to enable application isolation and multithreading.
  • Create and deploy manageable applications.
  • Create classes that can be serialized to enable them to be easily stored and transferred.
  • Create hardened applications that are resistant to attacks and restrict access based on user and group roles.
  • Use interoperability and reflection to leverage legacy code and communicate with other applications.
  • Write applications that send e-mail messages.
  • Create applications that can be used in different regions with different languages.
  • Draw charts and create images, and either display them as part of your application or save them to files.

Microsoft Press Web site (www.microsoft.com/learning/support/books/).

For information about ordering the full version of any Microsoft software, please call Microsoft Sales at (800) 426–9400 or visit www.microsoft.com.

Chapter 1: Framework Fundamentals

The .NET Framework is an integral Microsoft Windows component designed to support next-generation applications and services. Many fundamentals of the .NET Framework will be familiar to developers who have worked in other object-oriented development environments; however, the .NET Framework also includes many unique elements.
 
Exam objectives in this chapter:
  • Manage data in a .NET Framework application by using system types.
  • Implement .NET Framework interfaces to cause components to comply with standard contracts.
  • Control interactions between .NET Framework application components by using events and delegates.

Sunday, March 6, 2011

மாநகர் எரிந்தது!

புகாரில் புதிதாய் மணம் முடித்து புறத்தவள் ஒருத்தியிடம் புருஷனை கொடுத்து 
புதிராய் நின்றவள் கண்ணகி 

புணர்ந்தால் புதியவளாய் புத்தொளி பெற்றாள்
பழைய கணவனை புதியவனாய் கண்டு
புறநகர் மாமதுரையில் புதுவாழ்வு புனைந்தாள்

தொடக்கமே முடிவாய் போனது 
துயரமே புது உருவ மெடுத்து
விதியெனும் உருபெற்று வார்த்தையில் விளையாடியது

ஓர் எழுத்துபிழை நேர்ந்தது
உயிர் மெய்யை பிரிந்தது
ரணத்தை தந்தது மரணம்

கட்டியழுதாள் கதரிதுடிதாள்
அத்துடன் நின்றாளோ - இல்லை 
பொங்கி எழுந்தாள் புரட்சி பெண்ணாய் 

கோமகனை குற்றம் சாட்டினாள்
உடைத்து உரைத்தாள் உண்மையை 
உணர்ந்தான் உலகை விடுத்தான் உத்தமன் 

நெடுந்துயில் கொண்டான் நெடுஞ்செழியன்
தந்தையின் பாவம் மக்களை வதைத்தது 
மாநகர் எரிந்தது மாமதுரை மண்ணாய்போனது.