Lesson 2.2

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

Objective

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.

TypeRepresentsRangeDefault Value
byte8 bit unsigned integer type0 to 2550
sbyte8-bit signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
ushort16-bit unsigned integer type0 to 65,5350
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
uint32-bit unsigned integer type0 to 4,294,967,2950
long64-bit signed integer type-923,372,036,854,775,808 to 9,223,372,036,854,775,8070L
ulong64-bit unsigned integer type0 to 18,446,744,073,709,551,6150
float32-bit single-precision floating point type-3.4 x 1038 to + 3.4 x 10380.0F
double64-bit double-precision floating point type(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
decimal128-bit precise decimal values with 28-29 significant digits(-7.9 x 1028 to 7.9 x 1028) / 100 to 280.0M
char16-bit Unicode characterU +0000 to U +ffff'\0'
boolBoolean valueTrue or FalseFalse

Reference Type Data Type

Reference type data type doesn’t keep value directly. Instead of it keeps location of variables which stores values.

data type

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#

TypeSizeValues
stringVariable Length0-2 billion Unicode characters
object----

Summary

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.

BackNext