Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C#, each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
Data types in C# is mainly divided into three categories
- Value Data Types
- Reference Data Types
- Pointer Data Type
-
Value Data Types : In C#, the Value Data Types will directly store the variable value in memory and it will also accept both signed and unsigned literals. The derived class for these data types are System.ValueType. Following are different Value Data Types in C# programming language :
-
Signed & Unsigned Integral Types : There are 8 integral types which provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned form.
Alias Type Name Type Size(bits) Range Default Value sbyte System.Sbyte signed integer 8 -128 to 127 0 short System.Int16 signed integer 16 -32768 to 32767 0 Int System.Int32 signed integer 32 -231 to 231-1 0 long System.Int64 signed integer 64 -263 to 263-1 0L byte System.byte unsigned integer 8 0 to 255 0 ushort System.UInt16 unsigned integer 16 0 to 65535 0 uint System.UInt32 unsigned integer 32 0 to 232 0 ulong System.UInt64 unsigned integer 64 0 to 263 0 -
Floating Point Types :There are 2 floating point data types which contain the decimal point.
- Float: It is 32-bit single-precision floating point type. It has 7 digit Precision. To initialize a float variable, use the suffix f or F. Like, float x = 3.5F;. If the suffix F or f will not use then it is treated as double.
- Double:It is 64-bit double-precision floating point type. It has 14 – 15 digit Precision. To initialize a double variable, use the suffix d or D. But it is not mandatory to use suffix because by default floating data types are the double type.
Alias Type name Size(bits) Range (aprox) Default Value float System.Single 32 3.4 × 1038 to +3.4 × 1038 0.0F double System.Double 64 ±5.0 × 10-324 to ±1.7 × 10308 0.0D -
Decimal Types : The decimal type is a 128-bit data type suitable for financial and monetary calculations. It has 28-29 digit Precision. To initialize a decimal variable, use the suffix m or M. Like as, decimal x = 300.5m;. If the suffix m or M will not use then it is treated as double.
Alias Type name Size(bits) Range (aprox) Default value decimal System.Decimal 128 -7.9 × 1028 to 7.9 × 1028 0.0M -
Character Types : The character types represents a UTF-16 code unit or represents the 16-bit Unicode character.
Alias Type name Size In(Bits) Range Default value char System.Char 16 U +0000 to U +ffff ‘
-
Signed & Unsigned Integral Types : There are 8 integral types which provide support for 8-bit, 16-bit, 32-bit, and 64-bit values in signed or unsigned form.