In this article, we will learn about the Different Types of Built In Data Types in c#.
The Data type uses everywhere in programming languages like C#, Java. they all are strongly typed languages, you are to inform the compiler about which data types you wish to use every time you declare a variable, as you will see in the article about Datatypes. In this article, we will see the most used data types and how they work.
1] Boolean
The boolean data type can only hold true or false if you try to assign any other value to this variable apart from true or false then you will get a compiler error.
Here’s an example of a bool declaration:
bool isProfitable = true;
2] Byte
Type Range Size .NET type byte 0 to 255 Unsigned 8-bit integer System
Here’s an example of a Byte declaration:
byte age = 25;
3] Char
Type Range Size .NET type char U+0000 to U+FFFF Unicode 16-bit character System.Char The char keyword
Here’s an example of a char declaration:
char var1 = 'X'; // Character literal
char var2 = '\x0058'; // Hexadecimal
char var3 = (char)88; // Cast from integral type
char var4 = '\x0041'; // Unicode 'A'
4] Decimal
Type Approximate Range Precision .NET type decimal ±1.0 x 10-28 to ±7.9228 x 1028 28-29 significant digits System.Decimal The decimal keyword represents a 128-bit data type. the decimal type has more precision and accurately stores numeric data, which makes it appropriate for financial and monetary calculations. the Decimal stores large and small digits after the decimal place.
Here’s an example of a Decimal declaration:
double val1 = 8.2e127;
double val2 = 7982365.83658341; // also 7982365.83658341D
// or 7982365.83658341d
5] Double
Type Approximate range Precision .NET type double ±5.0 × 10−324 to ±1.7 × 10308 15-16 digits System.Double The double keyword type that stores 8-byte numeric large and small values floating-point values. It stores fractional values and It also requires more memory than an int. Single, Double
Here’s an example of a Double declaration:
double var1 = 8.2e127;
double var2 = 7982365.83658341; // also 7982365.83658341D
// or 7982365.83658341d
6] Enum
The enumeration type provides an efficient way to define a set of named integral constants that assign to the names or string values to integral constants
Here’s an example of a Enum declaration:
enum Months{ Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,Oct, Nov,Dec};
7] Float
Type Approximate range Precision .NET type float ±1.5 x 10−45 to ±3.4 x 1038 7 digits System.Single The value range is (- 3.40E + 38) -340282346638528859811704183484516925440.0000000000000000 —lowest (3.40E + 38)
340282346638528859811704183484516925440.0000000000000000 —highest
The float keyword type that stores 4-Byte floating-point numbers. It provides single-precision floating point number representation with the use of character suffix “f” or “F”. the Float is less precise than a double.
Here’s an example of a float declaration:
float price = 36592.73; // also 36592.73F
// or 36592.73f
float weight = 1.54e-15;
float speed = 3.21E3;
8] Integer
Type Range Size .NET type int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer System.Int32 The integer data type stores 4-Byte for storing numbers without decimals. When working with negative and positive numbers, Int is the most commonly used data type. Integers have several data types within C# like an Integer value
Data types include:- int (4byte), short (2byte), long (8byte), and byte (1byte), it depends on their size of the number to store.
Here’s an example of a int declaration:
int tradeDeficit = -2058293762;
9] Long
Type Range Size .NET type long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer System.Int64 The long data type stores 8 bytes. It is the size of 2 ints. It represents large integral numbers but not floating-points. the Integer literals representation with the use of character suffix “l” or “L”.
Here’s an example of a long declaration:
long negativeVariance = -1636409717646593274;
// also –1636409717646593274l
// or –1636409717646593274L
10] Short
Type Range Size .NET type short -32,768 to 32,767 Signed 16-bit integer System.Int16 The short data type stores 2 bytes half the size of an int. It reduces the memory usage of integers. The short can hold is -32768 And the largest is 32767.
Here’s an example of a short declaration:
short temperatureFarenheit = -36;
11] Struct
The Struct type is a value type,
Here’s an example of a struct declaration:
public struct Book
{
public decimal price;
public string title;
public string author;
}
12] UInt
Type Range Size .NET type
Here’s an example of a UInt declaration:
uint nationalPopulation = 4139276850; // also 4139276850u
or 4139276850U
13] Ulong
Type Range Size .NET type ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer System.UInt64 A Ulong is unsigned and has a range of 0 to 18,446,744,073,709,551,615. Unsigned long literals may optionally have a ul or UL suffix.
Here’s an example of a ulong declaration:
ulong lightYearsFromEarth = 72038289347236792;
// also 72038289347236792ul
// or 72038289347236792UL
14] UShort
Type Range Size .NET type
Here’s an example of a ushort declaration:
ushort numberOfJellyBeans = 62873;