C# Data Types

 A data type specifies the size and type of variable values.

It is prominent to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, besides it will also make your code more maintainable and readable. The most popular 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,807
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 stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are int and long. Which type you should use, depends on the numeric value.

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

There are many numeric types in C#, the most used for numbers are int (for complete numbers) and double (for floating point numbers). 

Integer Types

Int

The int data type can store complete numbers from -2147483648 to 2147483647. In general; the int data type is the preferred data type when we make variables with a numeric value.

Long

The long data type can store entire numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an “L”:

Floating Point Types

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.

The float and double data types can store fractional numbers. Note that you should end the value with an “F” for floats and “D” for doubles:

Use 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. Thus, 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 announces the power of 10:

Boolean

A boolean data type is informed with the bool keyword and can only take the values true or false

Characters

The char data type is used to store a single character. The character must be enclosed by single quotes, like 'A' or 'c':

Strings

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




Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Kotlin Strings: Features and Operations Guide

Kotlin Android Program (QCR) Application Codes That Read Text in Photos