How To Use Data Types In C#

16 mins read

The data type definition must be a valid C# data type that includes float, int, double, char or any other data type that a user may define. A data type specifies the size and type of variable values. It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, but it will also make your code more maintainable and readable.

C# mainly categorized data types in two types, which are:

  • Value types: Value types include simple types such as int, long, float, double, bool, and char, Nullable value type, struct types, enum types.
  • Reference types: include class types, interface types, delegate types, and array types.

Note: A variable in C# must be a specified data type.

These are the most common data types in C#:

Data Type Size Description
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single quotes
string 2 bytes per character Stores a sequence of characters, surrounded by double quotes

Numbers In C#

Number types are divided into two groups:

  • Integer types: this type 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: this represents numbers with a fractional part, containing one or more decimals. Valid types are float and double.

There are many numeric types in C#, but the most commonly used numbers are int which is for whole numbers, and double for floating point numbers.

Integer Types In C#

Before explaining the Integer Types, let me quickly explain to us some integral numeric types, this types represent integer numbers. All integral numeric types are value types. They’re also simple types and can be initialized with literals. All integral numeric types support arithmetic, bitwise logical, comparison, and equality operators.

Here are characteristics of the integral types:

C# type/keyword Range Size .NET type
sbyte -128 to 127 Signed 8-bit integer System.SByte
byte 0 to 255 Unsigned 8-bit integer System.Byte
short -32,768 to 32,767 Signed 16-bit integer System.Int16
ushort 0 to 65,535 Unsigned 16-bit integer System.UInt16
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer System.Int32
uint 0 to 4,294,967,295 Unsigned 32-bit integer System.UInt32
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer System.Int64
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer System.UInt64
nint Depends on platform (computed at runtime) Signed 32-bit or 64-bit integer System.IntPtr
nuint Depends on platform (computed at runtime) Unsigned 32-bit or 64-bit integer System.UIntPtr

Note: C# supports all the predefined integral types above.

In all the table rows except the last two, each C# type keyword from the leftmost column is an alias for the corresponding .NET type. The keyword and .NET type name are interchangeable. For example, the following declarations declare variables of the same type:

The nint and nuint types in the last two rows of the table are native-sized integers. Starting in C# 9.0, you can use the nint and nuint keywords to define native-sized integers. These are 32-bit integers when running in a 32-bit process, or 64-bit integers when running in a 64-bit process. They can be used for interop scenarios, low-level libraries, and to optimize performance in scenarios where integer math is used extensively.

The native-sized integer types are represented internally as the .NET types System.IntPtr  and System.UIntPtr  Starting in C# 11, the nint and nuint types are aliases for the underlying types.

The default value of each integral type is zero, .

Each of the integral types has MinValue and MaxValue properties that provide the minimum and maximum value of that type. These properties are compile-time constants, except for the case of the native-sized types ( nint and nuint). The MinValue and MaxValue properties are calculated at runtime for native-sized types. The sizes of those types depend on the process settings.

Use the System.Numerics.BigInteger  structure to represent a signed integer with no upper or lower bounds.

Now, let us discuss about int which represents the integer types. The int data type can store whole numbers from -2147483648 to 2147483647, as explained in the table above. The int data type is mostly the preferred data type when we create variables with a numeric value.

Code sample:

 

Output:

 

Now, let us discuss the  long data type. This data type can store whole numbers from -9223372036854775808 to 9223372036854775807. As discussed in the table above. The  long data type is used when int is not large enough to store the value.

Note: Always end the value of the long data type with “L”.

Code sample:

Output:

Floating Point Types In C#

The floating-point numeric types represent real numbers. All floating-point numeric types are value types. They are also simple types and can be initialized with literals. All floating-point numeric types support arithmetic, comparison, and equality operators.

Characteristics of the floating-point types in C#

C# type/keyword Approximate range Precision Size .NET type
float ±1.5 x 10−45 to ±3.4 x 1038 ~6-9 digits 4 bytes System.Single
double ±5.0 × 10−324 to ±1.7 × 10308 ~15-17 digits 8 bytes System.Double
decimal ±1.0 x 10-28 to ±7.9228 x 1028 28-29 digits 16 bytes System.Decimal

C# also supports all the predefined floating-point types above.

In the preceding table, each C# type keyword from the leftmost column is an alias for the corresponding .NET type. They are interchangeable.

Code sample

Let us declare variables of the same types:

The default value of each floating-point type is zero, . Each of the floating-point types has the MinValue and MaxValue constants that provide the minimum and maximum finite value of that type. The float and double types also provide constants that represent not-a-number and infinity values. For example, the double type provides the following constants: Double.NaN, Double.NegativeInfinity, and Double.PositiveInfinity.

The decimal type is appropriate when the required degree of precision is determined by the number of digits to the right of the decimal point. Such numbers are commonly used in financial applications, for currency amounts (for example, $1.00), interest rates (for example, 2.625%), and so forth. Even numbers that are precise to only one decimal digit are handled more accurately by the decimal type: 0.1, for example, can be exactly represented by a decimal instance, while there’s no double or float instance that exactly represents 0.1. Because of this difference in numeric types, unexpected rounding errors can occur in arithmetic calculations when you use double or float for decimal data. You can use double instead of decimal when optimizing performance is more important than ensuring accuracy. However, any difference in performance would go unnoticed by all but the most calculation-intensive applications. Another possible reason to avoid decimal is to minimize storage requirements. For example, ML.NET uses float because the difference between 4 bytes and 16 bytes adds up for very large data sets.

You can mix integral types and the float and double types in an expression. In this case, integral types are implicitly converted to one of the floating-point types and, if necessary, the float type is implicitly converted to double . The expression is evaluated as follows:

  • If there is double type in the expression, the expression evaluates to double, or to bool in relational and equality comparisons.
  • If there is no double type in the expression, the expression evaluates to float, or to bool in relational and equality comparisons.

You can also mix integral types and the decimal type in an expression. In this case, integral types are implicitly converted to the decimal type and the expression evaluates to decimal, or to bool in relational and equality comparisons.

You cannot mix the decimal type with the float and double types in an expression. In this case, if you want to perform arithmetic, comparison, or equality operations, you must explicitly convert the operands either from or to the decimal type, as the following example shows:

Note: You can either use standard numeric format strings or custom numeric format strings to format a floating-point value.

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: Always end the value with an “F” for floats and “D” for doubles.

Code sample for float:

Output:

Code sample for double:

Output:

Should I 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. Therefore, it is safe to use double for most calculations.

Code sample

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

Output:

Booleans In C#

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

To perform logical operations with values of the bool type, use Boolean logical operators. The bool type is the result type of comparison and equality operators. A bool expression can be a controlling conditional expression in the if, do, while, and for  statements and in the conditional operator ?:.

Note: The default value of the bool type is false.

Code sample:

Output:

Characters In C#

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

Code sample:

Output:

 

Strings In C#

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

Code sample:

Output:

Thanks for reading…

Happy Coding!

Leave a Reply

Your email address will not be published.