Data type in C#:
While Writing Program in any Language, We Need to use Various Variables to Store Various Information. Variables are Nothing but Reserved Memory Locations to Store Values. This Means that We Create a Variable, We Reserve some Space in Memory.
We May Like to Store Information of Various Data Types Like Character, Wide Character, Integer, Floating point, Double Floating Point, Boolean etc. Based on the Data Type of a Variable, The Operating system Allocates memory and Decides what ca be stored in The Reserved Memory.
In Computer Programming, Information is Stored in A computer Memory With Different data types. We Must Know what it is to be Stored in a Computer Memory, Whether it is just a Digit or Character, A Small Number, or Decimal Number. Computer Memory is Organized in Bytes, And for these Variables with varying Information a Data types is Associated. Byte is the Minimum Amount of Memory in Declared with two entities, Its type and Name.
Value Type:
Value type Variables can be Assigned a Value Directly. Assigning one Value type variable to Another copies the Contained value. This differs form the assignment of Reference type variables, which Copies a Reference to the object but not the Object it self. They are Derived from the class System.ValueType.
The Value Types directly Contain Data. Some Examples are Int, Char, Float etc. Which stores numbers, Alphabets, and Floating point Numbers, Respectively. When we declare an Int type, the System allocates memory to store the Value.
The Following Tables shows the Default Values of Values type.
Type | Represents | Range | Default Value |
---|---|---|---|
Bool | Boolean Value | True or False | False |
Byte | 8-bit Unsigned Integer | 0 to 255 | 0 |
Char | 16-bit Unicode Character | u +0000 to u +fff | '\0' |
Decimal | 128-bit Precise Decimal values with 28-29 significant digits. | (-7.9 x 1028 to 7.8 X 1028) | 0.0M |
Double | 64-bit Double-precision Floating Point Type | (+/-)5.0-324 to (+/-)1.7 x 10308 | 0.0D |
Float | 32-bit Single-precision Floating point type | -3.4 x 1038 to +3.4 x 1038 | 0.0F |
Int | 32-bit Signed Integer type | -2,147,483,648 to 2,147,483,647 | 0 |
long | 64-bit signed Integer type | -9,223,372,036,854,775,808 to 9,233,372,036,854,775,807 | 0L |
sbyte | 8-bit Signed integer type | -128 to 127 | 0 |
short | 16-bit signed integer type | -32,768 to 32,767 | 0 |
uint | 32-bit unsigned integer type | 0 to 4,294,967,295 | 0 |
ulong | 64-bit Unsigned integer type | 0 to 18,446,744,073,709,551,615 | 0 |
ushort | 64-bit unsigned integer type | 0 to 65,535 | 0 |
We can Get the Exact size of type or Variable on a Particular platform, We can use an Operator sizeof() to get the actual size of any type. This operator yields the Storage size of the object or type in bytes. Following is an Example to get the size of data type on any machine:
using System;
namespace SizeOf
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of Int: {0}", sizeof(int));
Console.WriteLine("Size of long: {0}", sizeof(long));
Console.WriteLine("Size of Double: {0}", sizeof(double));
Console.WriteLine("Size of sbyte: {0}", sizeof(sbyte));
Console.WriteLine("Size of Short: {0}", sizeof(short));
Console.WriteLine("Size of Uint: {0}", sizeof(uint));
Console.WriteLine("Size of Ulong: {0}", sizeof(ulong));
Console.WriteLine("Size of Ushort: {0}", sizeof(ushort));
Console.WriteLine("Size of Float: {0}", sizeof(float));
Console.WriteLine("Size of Byte: {0}", sizeof(byte));
Console.WriteLine("Size of Decimal: {0}", sizeof(decimal));
Console.ReadKey();
}
}
}
Size of Data types:
Size of Int: 4
Size of long: 8
Size of Double: 8
Size of sbyte: 1
Size of Short: 2
Size of Uint: 4
Size of Ulong: 8
Size of Ushort: 2
Size of Float: 4
Size of Byte: 1
Size of Decimal: 16
“
In Above Program, Data sizes might be difference from your. Its depends on the computer structure and the compiler. copy this code into your compiler and check whether its same or different from mine.
Pointer Type:
Pointers are the special type of Variables, which can store the Memory address of Another variables. They contain a Memory address, Not the Value of the Variable. Pointers in C# have the same capabilities as the pointers in C or C++. (Read more...)
Following are the Examples of Pointer type Declarations:
Syntax: | Description: |
---|---|
int* Ptr | Ptr is a pointer to an Integer. |
int** Ptr | Ptr is a pointer to a Pointer to an Integer. |
int*[] Ptr | Ptr is a Single-Dimensional array of Pointer to Integers. |
char* Ptr | Ptr is a pointer to Char. |
void *Ptr | Ptr is a Pointer to an Unknown Type. |
Reference Type:
The Reference Type variables is such type of variable in C# that holds the reference of memory address instead of value. In other words, they refer to a Memory location. Using Multiple variables, the reference types can refer to a Memory Location. If the data in the Memory location is changed by one of the variables, the other variable automatically reflects this change in value.