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 };

No comments:

Post a Comment