Value Types:
In C#, value types are used to store values and combine to design classes. These are built in data types.
These types are placed on the stack when we are using these types in the classes
The following value types are available in C#,
- Sbyte
- Short
- int
- long
- byte
- ushort
- uint
- ulong
- float
- double
- bool
- Char
- Decimal
Using Bool values:
Bool is a variable that can contain ‘true’ or ‘false’ and it occupies 1 byte. In C#, bool can store zero for false and nonzero value for true.bool value types can be illustrated in the following example.
Class BoolExample
{
Static void main (string[] args)
{
bool b= false;
if (b)
{
Console.writeline (b); //writes true
}
else
{
Console.writeline (b); //writes false
}
}
}
Using integers:
By using integer data types we can store integer values.’int’keyword is used to store integer values which has range in between -2,147,483,648 to 2,147,483,648.
In addition to int in C# , we have several data types for integer values
| Data type | Range | Memory |
| Short | -32,768 to 32,768 | 2 bytes( 16 bits) |
| ushort | 0 to 65,536 | 2 bytes(16 bits) |
| int | -2,147,483,648 to 2,147,483,648 | 4 bytes (32 bits) |
| uint | 0 to 4,294,967,296 | 4 bytes (32 bits) |
| long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 | 8 bytes |
| ulong | 0 to 18,446,744,073,709,551,616 | 8bytes |
Example:
Class IntegersExample
{
Static void main(string[] args)
{
int i=12345;
long l=345678;
Console.writeline (i);
Console.writeline (l);
}
}
Using char:
C# provides ‘char’ data type to store a single character or strings in array. ‘char’ is a Unicode character that occupies 2 bytes of memory.
Example:
Class CharExample
{
Static void main(string[] args)
{
Char ch=’A’;
Char[] ch1={‘H’,’a’,’I’};
Console.writeline (ch);
Console.writeline(ch1);
}
}
Using float, double, decimal:
In C#, we have float ,double, decimal to handle the fractional values.
A float is 4 bytes, and double is 8 bytes and decimal type holds 128 bits
Example:
Class floatExample
{
Static void main(string[] args)
{
float f= 3.2;
double d=5E-02;
Console.writeline(f);
Console.writeline(d);
}
}
No comments:
Post a Comment