Friday, 17 August 2018

C#: Built in Types

C# support almost all kind of data types to work with. All data types are represented as classes in System namespace. Data types can be categorized into following types.
a.   Boolean : bool
b.   Integer: byte, sbyte, int, uint, short, ushort, long, ulong
c.   Float: double, float, decimal
d.   Character : char
e.   String

Following table summarizes all the data types
Short Name
Class
Min value
Max value
byte
Byte
0
255
sbyte
SByte
-128
127
int
Int32
-2,147,483,648
2,147,483,647
uint
UInt32
0
4294967295
short
Int16
-32,768
32767
ushort
UInt16
0
65535
long
Int64
- 9223372036854775808
9223372036854775807
ulong
UInt64
0
18446744073709551615
float
Single
-3.402823e38
3.402823e38
double
Double
-1.79769313486232e308
1.79769313486232e308
decimal
Decimal
±1.0 × 10e−28
±7.9 × 10e28
char
Char

Represent Unicode character. It take 16 bits.
string
String

Sequence of characters

Note
a. Suffix Float variables by f.
         Ex:  float floatVar = 18.18f;

b. Suffix Decimal variables by m.
         Ex: decimal decimalVar = 20.20m;

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        byte byteVar = 10;
        sbyte sbyteVar = 11;
        int intVar = 12;
        uint uintVar = 13;
        short shortVar = 14;
        ushort ushortVar = 15;
        long longVar = 16;
        ulong ulongVar = 17;
        float floatVar = 18.18f;
        double doubleVar = 19.19;
        decimal decimalVar = 20.20m;
        char charVar = 'S';
        string stringVar = "Hello World";

        Console.WriteLine("byteVar = {0}", byteVar);
        Console.WriteLine("sbyteVar = {0}", sbyteVar);
        Console.WriteLine("intVar = {0}", intVar);
        Console.WriteLine("uintVar = {0}", uintVar);
        Console.WriteLine("shortVar = {0}", shortVar);
        Console.WriteLine("ushortVar = {0}", ushortVar);
        Console.WriteLine("longVar = {0}", longVar);
        Console.WriteLine("ulongVar = {0}", ulongVar);
        Console.WriteLine("floatVar = {0}", floatVar);
        Console.WriteLine("doubleVar = {0}", doubleVar);
        Console.WriteLine("decimalVar = {0}", decimalVar);
        Console.WriteLine("charVar = {0}", charVar);
        Console.WriteLine("stringVar = {0}", stringVar);
    }
}


Output




Previous                                                 Next                                                 Home

No comments:

Post a Comment