Value types
Contain their data directly instead of using a reference
Value types are stored on the stack instead of the heap
3 value types
- Built-in types
- User-defined types
- Enumerations
Built-in types
- Numeric data types
- Boolean types
Reasons to choose between numeric types
- Size of the value
- Precision you need
Built-in value types:
Visual Basic Alias |
C# Alias |
Range |
SByte |
sbyte |
-128 to 127 |
Byte |
byte |
0-255 |
Short |
short |
-32768 to 32767 |
Integer |
int |
-2.1billion to 2.1 billion |
Uinteger |
uint |
0 to 4.2 billion |
Long |
long |
-922 quatrillion to 922 quatrillion |
Single |
float |
-3.4 E+38 to 3.4 E+38 (allow decimal) |
Double |
double |
1.7 E+308 to 1.7 E+308 (allow decimal) (most efficient for decimals) |
Decimal |
decimal |
29 places negative and positive (no decimal) |
Other value types
Visual Basic Alias |
C# Alias |
|
Char |
Char |
Single characters |
Boolean |
Bool |
True/false |
Date |
Date |
1/1/0001 to 12/31/9999 |
Declare value type
Declare a variable as an instance of that type
The constructor assigns a default value of 0 or null
VB:
Dim x as Boolean = true
Commonly used types:
- Integers
- Integers are whole numbers
- They have no decimal places
- Ex:
- Dim myInteger as Integer=10
- Decimal
- Use for large precision
- Financial and scientific calculations
- Double
- Use for all other decimal numbers
- Boolean
- True or false
- Example
- Dim result as Boolean = true