In: Computer Science
Explain different types of data used in C#.•Illustrate and give examples of all predefined data types in C#.
C sharp
There are 2 types of data in C# -
1. Predefined Data Types: These data types are already defined
in C# by default, such as Integer, Boolean, Float, etc.
2. User-defined Data Types: These data types are defined by used in
C# such as Structure, Enumerations, etc.
The predefined data types in C# are given below-
1. byte
It is a data type used to represent 8-bit unsigned integers. It is an immutable value type and It ranges from 0 to 255. You can perform mathematical and bitwise operations on them.
Eg. byte value1 = 255;
2. sbyte
It is a data type that represents an 8-bit signed integer. It represents integers with values ranging from -128 to 127.
Eg. sbyte value2 = -128;
3. short
It is a data type used to represent a 16-bit signed integer. It ranges from -32,768 to 32,767.
Eg. short value3 = -32768;
4. ushort
It is a data type used to represent a 16-bit unsigned integer. It ranges from 0 to 65,535.
Eg. ushort value4 = 32768;
5. int
It is a data type used to represent a 32-bit signed integer. It ranges from -2,147,483,648 to 2,147,483,647.
Eg. int value5 = -2147483648;
6. uint
It is a data type used to represent a 32-bit unsigned integer. It ranges from 0 to 4,294,967,295.
Eg. uint value5 = 2147483648;
7. long
It is a data type used to represent a 64-bit signed integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Eg. long = -9223372036854775808;
8. ulong
It is a data type used to represent a 64-bit unsigned integer. It ranges from 0 to 18,446,744,073,709,551,615.
Eg. long = 9223372036854775808;
9. float
It is a data type used to represent a 32-bit Single-precision floating point. It's positive values are 1.4 x 10-45 to 3.4 x 1038 and negative values are -3.4 x 1038 to -1.4 x 10-45.
Eg. float f1 = 123456.5F;
float f2 = 1.123456f; //(Use f or F suffix with literal to make it float type)
10. double
It is a data type used to represent a 64-bit double-precision floating point. It's positive values are 4.9 x 10-324 to 1.8 x 10308 and negative values are -1.8 x 10308 to -4.9 x 10-324.
Eg. double value1 = 12345678912345.5d;
double value2 = 1.123456789123456d; //(Use d or D suffix with
literal to make it double type.)
11. decimal
It is a data type used to represent a 128-bit decimal type for financial and monetary calculations. Its positive values are 1.0 x 10-28 to 7.9 x 1028 and negative values are -7.9 x 1028 to -1.0 x 10-28.
Eg. decimal value1 = 123456789123456789123456789.5m;
decimal value2 = 1.1234567891345679123456789123m; //(Use m or M
suffix with literal to make it decimal type)
12. char
It is a data type used to represent a 16-bit single Unicode character. Its range can include any character from U +0000 to U +ffff
Eg. char ch = 'H';
13. bool
It is a data type used to represent an 8-bit logical true/false value. Its value can be either True or False, true is represented internally by 1, false is represented by 0
Eg. bool isFunction1 = true;
bool isFunction2 = false;
14. object
It is a data type used as a Base type of all other types
Eg. object objItem = 100;
15. string
It is a data type used to represent a sequence of Unicode characters. A string variable in C# cannot be declared as a fixed length.
Eg. string greeting = "Hello";
16. DateTime
It is a data type used to represent data and time, it's size can be 64-bit. It can have the values from 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999.
Eg. DateTime dt4 = new DateTime(2011,08,4,4,09,20,200);