Built-In Data Types In C#

C# offers a wide variety of built-in data types. Whenever you create a variable for storing values - you need to define its type. C# being a strongly typed language - enforces type checking when you assign a value to a variable. In simple words, you cannot assign a string to an integer variable.



These data types can be broadly classified based on the type of values - Boolean, Integer, Character, Floating Point. Each built-in type has a specific size which is fixed. Let's look at each category in detail -

1. Boolean (bool) - For storing values like True or False, use this data type for your variables. This type is used in conditional statements where you want your code to take the decision. For e.g.

 - Is It Raining?  - True Or False
 - y > 10 ? - True Or False

For this, use bool.

2. Integer - For storing integer values i.e. numbers without decimal points. There are different types in C# based on whether you want to store negative numbers or range of numbers is high or small. For e.g.

- To store Age of a person - Always a positive integer and generally it is not greater than 150. You can use byte.
- For scenarios, where you have a small range with negative numbers allowed. You can use - sbyte

3. Character (char) -  For storing single character. It stores a Unicode format and it is just a 16 bit integer value. Every character has unique integer value associated with it. For e.g. 'A' has a value of 65. Use char data type if you want to store a single character.

4. Floating Point - For storing numbers with or without decimal point. Different variants of this data type exist based on the precision you require for your decimal numbers. C# has float, double, decimal with increasing precision values.

NOTE: u means unsigned so uint is unsigned int or ulong is unsigned long and s means signed so sbyte is signed byte.

Refer below list for the data types available for storing values.

 Type 
 Size (in bytes)  
 Range
 byte 1 0 to 255 
 sbyte  1 -128 to 127 
 short  2 -32,768 to 32,767
 ushort  2 0 to 65,535
 int  4 -2,147,483,648 to 2,147,483,647 
 uint  4 0 to 4,294,967,295
 long  8 -923,372,036,854,775,808 to 9,223,372,036,854,775,807 
 ulong  8 0 to 18,446,744,073,709,551,615
 char  2 Unicode Characters (U +0000 to U +ffff)
 float  4 -3.402823e38 to 3.402823e38
 double  8 -1.79769313486232e308 to 1.79769313486232e308
 decimal         16    1e–28 to 7.9e+28
 bool  1 True or False


Do I Need To Remember All These Ranges?


No, you do not need to remember all these large numbers. Just remember the names of the data types with size. Range of these can be easily calculated by the number of bytes they occupy. Let us take an example of how to calculate the range - int takes 4 bytes and allows negative number.


1 byte  = 8 bits
4 byte  = 8 * 4 = 32 bits

1 bit can represent 2 values i.e. 0 and 1
2 bit can represent 4 values i.e. 0, 1, 2 and 3
3 bit can represent 8 values i.e. 0, 1, 2, 3, 4, 5, 6 and 7
and so on

32 bits can represent  232  = 4294967296
As shown above - 4294967296 values can be represented if we have 4 bytes. If you again refer the table, you can check that the range of uint(4 bytes) is 0 to 4294967295 because 0 is also considered in the range and thus total values are 4294967296 - 1 = 4294967295.

In case of int,  we have negative numbers as well. So range is calculated as -

4294967296 ÷ 2 = 2147483648 and since 0 is also considered, we subtract 1 from positive range. Therefore we have -2147483648 to 2147483647. Refer table again.

So much pain just to calculate the range? Easy Way 


You can find the Max value and Min value of data types using int.MaxValue and int.MinValue in your C# code.

cheezycode-csharp-data-types


Let us know if you have any query. Happy Reading.(refer full series here - C# Tutorial Index)




Comments

Popular posts from this blog

Create Android Apps - Getting Started

Kotlin Type Checking and Smart Cast with Examples