Learn Visual C#
Defining Data Types in C# with Example
Here we are learning very important topics in C# programming – Data Types. In this chapter we will learn
√ What is Data Types in C#?
√ How many kinds of data types are in C#?
√ How to use data types in programming language.
What is Data Types in C#?
"Data Type in C# is a measurement of memory that tells compiler how much memory required for assigned variables."
For example, if you create and integer variable intNumber then it is needed 32 bit space in memory. C# is a strongly typed language and you cannot use variables without assigning appropriate data type.
How many kinds of data types in C#?
There are two kinds of data types in C# - Value Type and Reference Type. All the data types are categorized with value type and reference type.
Value Type Data Type
A value type data type directly contains data. If you assigned a value type variable then it creates a location in memory according to data type and store value on them. Int, char, float are the examples of value type data type. Here is the complete list of value type of data type with their memory size and capacity of holding values.
|byte||8 bit unsigned integer type||0 to 255||0|
|sbyte||8-bit signed integer type||-128 to 127||0|
|short||16-bit signed integer type||-32,768 to 32,767||0|
|ushort||16-bit unsigned integer type||0 to 65,535||0|
|int||32-bit signed integer type||-2,147,483,648 to 2,147,483,647||0|
|uint||32-bit unsigned integer type||0 to 4,294,967,295||0|
|long||64-bit signed integer type||-923,372,036,854,775,808 to 9,223,372,036,854,775,807||0L|
|ulong||64-bit unsigned integer type||0 to 18,446,744,073,709,551,615||0|
|float||32-bit single-precision floating point type||-3.4 x 1038 to + 3.4 x 1038||0.0F|
|double||64-bit double-precision floating point type||(+/-)5.0 x 10-324 to (+/-)1.7 x 10308||0.0D|
|decimal||128-bit precise decimal values with 28-29 significant digits||(-7.9 x 1028 to 7.9 x 1028) / 100 to 28||0.0M|
|char||16-bit Unicode character||U +0000 to U +ffff||'\0'|
|bool||Boolean value||True or False||False|
Reference Type Data Type
Reference type data type doesn’t keep value directly. Instead of it keeps location of variables which stores values.
In the preceding picture, there are two variables defined. int num is value type data type and object obj is the reference type data type. int num keeps a value "25" and the location of num variable is 0x000323. Object obj variables does not keep direct value of variable num, instead of it keeps the address of num variable as 0x000323. So, in the program whenever value of num variable changed, it automatically reflects in obj variable.
There are two types of reference type data type in C#
|string||Variable Length||0-2 billion Unicode characters|
In this lesson, we have learned about data types as what is data type, how many types of data types in c sharp, value type and reference type. In the next few chapters we will learn all data types with easy programming examples. In the next chapter we will learn about byte data type.