Datatypes in C#
Last updated
Was this helpful?
Last updated
Was this helpful?
In programming, data types are declarations for variables. This determines the type and size of data associated with variables. most use datatype is int, string, char.
In C#, the byte is used to represent unsigned integers. It is an immutable value type and the range of byte is from 0 to 255(2⁸=256).one byte has 8 bit. In the computer data storage in 0 1 format. This class allows you to create Byte data types and you can perform mathematical and bitwise operations on them like addition, subtraction, multiplication, division, XOR, AND etc.
Here b variable is assigned decimal equivalent .then it converted into character in “k” because in ASCII table 75 is assign Character “k”.
So, if you want one-byte value then use the byte data type and if you want the maximum value of byte then use byte.MaxValue and for MinValue then use byte.MinValue.
It is the second data type in C#. its a two-byte length datatype that can contain Unicode data.
Here char has two-byte length 2¹⁶=65536.Its uses for only a single Character.
The string data type is an array of characters.string keyword is used for declaring the variable. The string keyword is an alias for the System.String class.
Note: single character use char data type. and for a series of characters then use string. Just read raw data like an image file then use byte data type.
Numeric datatype without decimal for signed number has three data types:
Int16, Int32, Int64 are used to represent signed integer with values ranging based on their occupied size in the memory.
Int16: It represents 16-bit (2byte)signed integer and it occupies 16-bit space in memory. Capacity is -32768 to +32767.(2¹⁶=65536/2=32768).
Int16 is nothing but short nshort=123.
Int32: It represents 32-bit (4byte)signed integer and it occupies 32-bit space in memory. Capacity is -2147483648 to +2147483647.(2³²=4294967296/2=2147483647).
Int32 is nothing but int no=123.
Int64: It represents 64-bit (8byte)signed integer and it occupies 64-bit space in memory. Capacity is -9223372036854775808 to +9223372036854775807.(2⁶⁴=18446744073709551616/2=9223372036854775807).
Int64 is nothing but Long no=123.
Numeric datatype without decimal for unsigned number has three data types:
Uint16: It represents 16-bit (2byte)unsigned integer and it occupies 16-bit space in memory. Capacity is 0 to 65536.(²¹⁶=65536).
UInt16 is nothing but ushort nshort=123;
UInt32: It represents 32-bit (4byte)unsigned integer and it occupies 32-bit space in memory. Capacity is 0 to 4294967296.(2³²=4294967296).
UInt32 is nothing but uint no=123;
UInt64: It represents 64-bit (8byte)signed integer and it occupies 64-bit space in memory. Capacity is 0 to 18446744073709551616.(2⁶⁴=18446744073709551616).
UInt64 is nothing but ulong no=123;
Note-depending on what kind of range then choose the data type.
Numeric datatype with a decimal for unsigned number has three data types:
Float: It is a 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.23235F; If the suffix F or f will not use then it is treated as double.
Double: It is a 64-bit double-precision floating-point type. It has 16 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.
Decimal: 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.
Note-When you want financial calculation then use decimal data type and when writing some logic for scientific equipment like 100⁰ degree then use double or float or in decimal calculation use float and double.
Bool: a Boolean or bool is a data type that has two possible values: it is either true or false.
DateTime: This DateTime data type represents an instance in time, typically expressed as a date and time of day.