Data types:
Data type specifies size and type of data.
Computer Memory capacity is measured in bytes
1 byte = 8bits(01010101)
1 bit = 0 (or) 1
Data types in C#.Net can be categorized into 5 types
I) Numeric datatype:
Numerical
A) Signed:
1)sbyte---1byte
1 sbyte: Holds 8-bit signed integers. The s in sbyte stands for signed, meaning that the variable's value can be either positive or negative. The smallest possible value for an sbyte variable is -128; the largest possible value is 127
2)short----2 bytes
2 short: Holds 16-bit signed integers. The smallest possible value for a short variable is -32,768; the largest possible value is 32,767.
3)int---4 bytes
3 int: Holds 32-bit signed integers. The smallest possible value of an int variable is -2,147,483,648; the largest possible value is 2,147,483,647.
4)long---8 bytes
Ø long: Holds 64-bit signed integers. The smallest possible value of a long variable is 9,223,372,036,854,775,808; the largest possible value is 9,223,372,036,854,775,807.
B)Unsigned:
1)Byte --- 1 byte
4 byte: Holds 8-bit unsigned integers. Unlike sbyte variables, byte variables are not signed and can only hold positive numbers. The smallest possible value for a byte variable is 0; the largest possible value is 255.
2) ushort--- 2 bytes
Ø ushort: Holds 16-bit unsigned integers. The u in ushort stands for unsigned. The smallest possible value of an ushort variable is 0; the largest possible value is 65,535.
3)uint--- 4 bytes
5 uint: Holds 32-bit unsigned integers. The u in uint stands for unsigned. The smallest possible value of a uint variable is 0; the largest possible value is 4,294,967,295.
4)ulong---8 bytes
6 ulong: Holds 64-bit unsigned integers. The u in ulong stands for unsigned. The smallest possible value of a ulong variable is 0; the largest possible value is 18,446,744,073,709,551,615.
VB.Net doesn’t have unsign data types.
II)Floating point datatype:
1)Float--- 4 bytes
7 float: Holds a 32-bit signed floating-point value
2)Double--- 8 bytes
8 double: Holds a 64-bit signed floating-point value
3)Decimal--- 16 bytes
9 decimal: Holds a 128-bit signed floating-point value
III)Character related:
1)Char- 2 bytes(with Unicode characters)
10 char: Holds 16-bit Unicode characters. The smallest possible value of a char variable is the Unicode character whose value is 0; the largest
possible value is the Unicode character whose value is 65,535.
Unicode characters:
11 Fundamentally, computers just deal with numbers. They store letters and other characters by assigning a number for each one. Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language.
1V)Logical data types:
-
1)bool--- 1 byte
12 bool: Holds one of two possible values, true or false.
It can hold only true/false as values.
V)General data types:
1)String:
13 string: Represents a string of Unicode characters. It allows easy manipulation and assignment of strings
2)Object:
14 object: Represents a general purpose type. In C#, all predefined and user-defined types inherit from the object type or System.Object class.
String and Object data types do not have predefined sizes.
Value & Reference types:
Based on the storage C# data types can be categorized into 2 category.
-Value type
1 Stack based
In the case of value type, memory is allocated on the stack which works on LIFO(Last in first out) approach as far as memory allocation and de-allocation is concerned.
Stack is a data structure, which works on LIFO approach.
Data structure defined as collection of related data.
-Reference type
2 Heap based
Reference type supports heap-based memory allocatin,memory is allocated in the free pool of unused area called the heap .The size of the memory is determined at run-time, and the memory de-allocation is subjected to Garbage Collector.
In contrast, the heap can be pictured as a random jumble of objects. Its advantage is that it allows objects to be allocated or deallocated in a random order. As we’ll see later, the heap requires the overhead of a memory manager and garbage collector to keep things in order
Difference between Value types and Reference types
Value Types Reference Types
1)Memory is allocated on the Stack | 1)Memory is allocated on the heap |
2)A value type variable contains the data itself. | 2)Reference type variable contains the address of memory location where data is actually stored. |
3)Examples for Value types a)Primitive data type b)Structures c)Enumerators | 3)Examples for reference types a)Classes b)Interfaces c)Delegates
|