Monday, 22 September 2014

C# - Data Types

Data types:

In general, data type can be simply understood as classification of data into its types or category. In programming languages, data type tells compiler what kind of data it it going to work on.

For example:
My name 'XYZ' would be a collection of alphabets only and my age is going to be a numeric value.

In C# there are a bunch of old and new data types available for you to use, few most commonly data types are:

Int, Character, String, Float, Double

Whatever data type we use, it will of-course consume some memory and by memory we mean some space in RAM (Random Access Memory). So using precise data type in our program is good for us. Our goal should be to use less memory and fast program execution.

List of some C# data types and size in bytes with their ranges:


'Long' and 'Int' are two similar data types to hold numeric values without any decimal point. i.e 1, 10, 256, 5145 but using 'Int' would be more efficient since it uses half the size of 'Long'.

In decimal values you may use 'Double' instead of 'float'. Though they both hold decimal values but use 'Double' because it can hold more decimal precission values (i.e digits after decimal) then 'float'. For normal work like, weight, speed of car use float. For scientific value which requires more precision like the value of pi, acceleration, mass of electron, etc, use Double.

So each data type is used according to its requirement. Choose wisely! :)

1 comment: