find and solve || findandsolve.com
Please wait.....

Data Types in C#

Size and type of variable values are specified by a datatype. For the corresponding variable, it is important to use correct data type and also it is used to avoid errors, save time and memory as well as make the code more maintainable and readable. Most common data types are:


Data TypeSizeDescription
int4 bytesStores whole numbers from -2, 147, 483,648 to 2,147,483,647
Long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223, 372,036,854,775,808
Float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
Bool1 bitStores true or false values
Char2 bytesStores a single character/letter, surrounded by single quotes
String2 bytes per characterStores a sequence of characters, surrounded by double quotes


Numbers

Number types are divided into two groups:

Integer types- it stores whole numbers, either positive or negative (such as 456 or -789) and it should be without decimals. int and long are its valid types. Which type should be used depends upon the numeric value.

Floating point types – it represents numbers with a fractional part, containing one or more decimals. float and double are its valid types.


Integer Types

Int- The int data type can store whole numbers from -2147483648 to 2147483647. In general, when we create variables with a numeric value, preferred data type is int.

Example
int myNum = 10000;
Console.WriteLine(myNum);
output

10000

Long- The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store value. We should end the value with “L”:

Example
long myNum = 160000000000000L;
Console.WriteLine(myNum);
output

160000000000000


Floating Point Types

Floating point type used be used whenever we need a number with a decimal, such as 5.16 or 3.14512.

Float- The float data type can store fractional numbers from 3.4e-038 to 3.4e+038. We should end the value with “F”:

Example
float myNum = 6.75F;
Console.WriteLine(myNum);
output

6.75

Double- The double data type can store fractional numbers from 1.7e-308 to 1.7e+308. We can end the value with a “D”(it may not be required):

Example
double myNum = 18.88D;
Console.WriteLine(myNum);
output

18.88

Which is better to use either float or double?

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.


Scientific Numbers

A floating point number can also be a scientific number with an “e” to indicate the power of 10:

Example
float  f1 = 35e3F;
double d1 = 12E4D;
Console.WriteLine(f1);
Console.WriteLine(d1);


Booleans

A Boolean data type is declared with the bool keyword and can take the values true or false:

Example
bool isCFun = true;
bool isFishTashy = false;
Console.WriteLine(isCFun); // Outputs True
Console.WriteLine(isFishTashy); // Outputs False


Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘b’:

Example
char myGrade = ‘A’;

Console.WriteLine(myGrade);
output

A


Strings

The string data type is used to store a sequence of characters(text). String values must be surrounded by double quotes:

Example
string greeting = “Hello World!”;

Console.WriteLine(greeting);

output

Hello World!