Download Data Types and Variables in C#: Numeral Types, Text Types, and Type Conversion and more Slides Programming Languages in PDF only on Docsity!
Numeral Types, Text Types and Type Conversion
Data Types and Variables
Table of Contents
1. Data Types and Variables
2. Integer and Real Number Type
3. Type Conversion
4. Boolean Type
5. Character and String Type
Computers are machines that process data
Instructions and data are stored
in the computer memory
How Computing Works?
Variables have name, data type and value
Assignment is done by the operator "="
Example of variable definition and assignment in C#
When processed, data is stored back into variables
Variables
Data type int count = 5;
Variable name
Variable value
A data type has: Name (C# keyword or .NET type) Size (how much memory is used) Default value Example: Integer numbers in C# Name: int Size: 32 bits (4 bytes) Default value: 0 Data Type Characteristics
int: sequence of 32
bits in the memory
int: 4 sequential bytes
in the memory
Always refer to the naming conventions
of a programming language – for C# use camelCase
Preferred form: [Noun] or [ Adjective] + [Noun]
Should explain the purpose of the variable (Always
ask yourself " What this variable contains? ")
Naming Variables
firstName, report, config, fontSize, maxSpeed
foo, bar, p, p1, LastName, last_name, LAST_NAME
Variable span is how long before a variable is called
Always declare a variable as late as possible (e.g. shorter span)
static void Main()
string outer = "I'm inside the Main()";
for (int i = 0; i < 10; i++)
string inner = "I'm inside the loop";
Console.WriteLine(outer);
//Console.WriteLine(inner); Error
Variable Span
"outer"
variable span
for (int i = 0; i < 10; i++)
string inner = "I'm inside the loop";
string outer = "I'm inside the Main()";
Console.WriteLine(outer);
// Console.WriteLine(inner); Error
Shorter span simplifies the code
Improves its readability and maintainability
Keep Variable Span Short
"outer" variable
span – reduced
Type Default Value Min Value Max Value Size sbyte 0 - 128 (- 2 7 ) 127 ( 2 7
**- 1 ) 8 bit byte 0 0 255 ( 2 8
- 1 ) 8 bit short 0 - 32768 (- 2 15 ) 32767 ( 2 15
- 1 ) 16 bit ushort 0 0 65535 ( 2 16
- 1 ) 16 bit int 0 - 2147483648 (- 2 31 ) 2147483647 ( 2 31
- 32 bit uint 0 0 4294967295 ( 2 32
- 1 ) 32 bit long 0
- 9223372036854775808 (- 2 63 ) 9223372036854775807 ( 2 63
- 1 ) 64 bit ulong 0 0 18446744073709551615 ( 2 64
- 1 ) 64 bit Integer Types**
Depending on the unit of measure we can use different data types:
Centuries – Example
byte centuries = 20;
ushort years = 2000;
uint days = 730484;
ulong hours = 17531616;
Console.WriteLine(
"{0} centuries = {1} years = {2} days = {3} hours.",
centuries, years, days, hours);
//20 centuries = 2000 years = 730484 days = 17531616 hours.
Examples of integer literals:
The '0x' and '0X' prefixes mean a hexadecimal value
E.g. 0xFE, 0xA8F1, 0xFFFFFFFF
The 'u' and 'U' suffixes mean a ulong or uint type
E.g. 12345678U, 0U
The 'l' and 'L' suffixes mean a long
E.g. 9876543L, 0L Integer Literals
Read four integers
Add first to the second
Divide the sum by the third number (integer division)
Multiply it by the fourth number
Print the result
Problem: Integer Operations 10 20 3 3 30 20 30 2 3 75 15 14 2 3 42
Real Number Types
Floating-point types:
Represent real numbers, e.g. 1.25, - 0.
Have range and precision depending
on the memory used
Sometimes behave abnormally in the calculations
May hold very small and very big values like
0.00000000000001 and
What are Floating-Point Types?