Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing



Data Types in C#:

I. What is a data type ? : [Starts at: 00min:00sec]
II. Kinds of C# data types : [Starts at: 04min:06sec]
III. Size and limits of C# data types : [Starts at: 09min:41sec]
IV. Choosing C# data types : [Starts at: 15min:09sec]

I. What is a data type ? : [Starts at: 00min:00sec]

Data type indicates:
The type of data to be stored in a variable or a constant
The size of memory to be allocated to hold the data.
Type of operations can be performed on the data present in the variable.

int : Data type
Type of data: Integers (whole numbers)
Size of memory: 4 bytes
Type of operations: +, -, *, /, %, == etc.

String: Data type
Type of data: Textual data
Size of memory: Numbers of characters * 2 bytes
Type of operations: ToUppercase, Join, Compare, etc.

II. Kinds of C# data types : [Starts at: 04min:06sec]

There are 2 different types of data types in C#

1) Primitive Data Types (Basic) :
A) Value types: hold actual value
a) Numeric data types:
i) Integer (whole numbers):
Integer numbers indicate numbers without floating point
byte(1) : bydefault unsigned (only +ve)
ushort / short(2),
uint / int(4),
ulong / long(8)
Ex: 22, 156 etc.

ii) Real (fractional numbers):
Real numbers indicate numbers with floating point
Ex: 3.142, 44.44 etc.

b) Non numeric data types:
bool(1) : Boolean values
Ex: true or false
char(2) : Character enclosed in single quotes
Ex: 'A' ,'$', '7' etc.

B) Reference types : hold the reference of other memory location
string(#chars *2) : Sequence of characters enclosed in double quotes
"Hello", "Hello 2" etc.

2) Non Primitive Data types (Derived):
Other data structures (Stack, Queue etc.)

III. Size and range of C# data types : [Starts at: 09min:41sec]

A smallest unit of memory is a bit.
Bit : indicates 0 or 1
Nibble: Sequence of 4 bits. Ex:1111

Sequence of 8 bits. Ex:00001111
Size of Byte is: 8 bits

Finding range of C# data types formulas:

Formula for finding total values possible to store:
2 pow size_of_data_type_in_bits

Total values possible to store in Byte memory location:
2 pow 8 = 256 values

Formula for finding Min and Max values possible to store:

Unsigned datatype:
Min = 0
Max = (2 pow size_of_data_type_in_bits) - 1

Unsigned Byte:
Byte min = 0 and Byte max = (2 pow 8) -1
Byte min = 0 and Byte max = 255

Signed datatype:
Min = - (2 pow size_of_data_type_in_bits - 1)
Max = + (2 pow size_of_data_type_in_bits - 1) - 1

Signed Byte:
Byte min = -(2 pow 8-1) and Byte max = (2 pow 8-1) -1
Byte min = -128 and Byte max = 127

IV. Choosing C# data types : [Starts at: 15min:09sec]

As we create games for low end devices like mobiles,
we must optimize the memory allocation
by choosing the correct data type for storing specific type of data

Think about type of data, range of data, sign and size of data

Score (min=0 and max= 100):
To create Score variable best is Byte data type

Range of data:
W.K.T. Byte data type min=0 and max=255,
So we can store 0 to 100 in Byte memory location

W.K.T a Byte memory location is enough to store 0 to 100

Type of data:
0 to 100 is numeric data,
Byte memory location can store numeric data

As 0 to 100 are unsigned values
Byte is by default unsigned