SlideShare a Scribd company logo
Primitive Data
Types and Variables
Integer, Floating-Point, Text
Data, Variables, Literals
Svetlin Nakov
Technical Trainer
www.nakov.com
Software University
http://softuni.bg
Table of Contents
1. Primitive Data Types
ο‚§ Integer
ο‚§ Floating-Point / Decimal Floating-Point
ο‚§ Boolean
ο‚§ Character
ο‚§ String
ο‚§ Object
2. Declaring and Using Variables
ο‚§ Identifiers, Variables, Literals
3. Nullable Types
2
Primitive Data Types
4
ο‚§ Computers are machines that process data
ο‚§ Data is stored in the computer memory in variables
ο‚§ Variables have name, data type and value
ο‚§ Example of variable definition and assignment in C#
ο‚§ When processed, data is stored back into variables
How Computing Works?
int count = 5;Data type
Variable name
Variable value
5
ο‚§ A data type:
ο‚§ Is a domain of values of similar characteristics
ο‚§ Defines the type of information stored in the computer memory
(in a variable)
ο‚§ Examples:
ο‚§ Positive integers: 1, 2, 3, …
ο‚§ Alphabetical characters: a, b, c, …
ο‚§ Days of week: Monday, Tuesday, …
What Is a Data Type?
6
ο‚§ 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
Integer Types
8
ο‚§ Integer types:
ο‚§ Represent whole numbers
ο‚§ May be signed or unsigned
ο‚§ Have range of values, depending on the size of memory used
ο‚§ The default value of integer types is:
ο‚§ 0 – for integer types, except
ο‚§ 0L – for the long type
What are Integer Types?
9
ο‚§ sbyte (-128 to 127): signed 8-bit
ο‚§ byte (0 to 255): unsigned 8-bit
ο‚§ short (-32,768 to 32,767): signed 16-bit
ο‚§ ushort (0 to 65,535): unsigned 16-bit
ο‚§ int (-2,147,483,648 to 2,147,483,647): signed 32-bit
ο‚§ uint (0 to 4,294,967,295): unsigned 32-bit
ο‚§ long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807):
signed 64-bit
ο‚§ ulong (0 to 18,446,744,073,709,551,615): unsigned 64-bit
Integer Types
10
ο‚§ Depending on the unit of measure we may use different data
types:
Measuring Time – Example
byte centuries = 20; // A small number (up to 255)
ushort years = 2000; // A small number (up to 32767)
uint days = 730480; // A large number (up to 4.3 billions)
ulong hours = 17531520; // A very big number (up to 18.4*10^18)
Console.WriteLine(
"{0} centuries is {1} years, or {2} days, or {3} hours.",
centuries, years, days, hours);
Integer Types
Live Demo
Floating-Point and
Decimal Floating-Point Types
13
ο‚§ Floating-point types:
ο‚§ Represent real numbers
ο‚§ May be signed or unsigned
ο‚§ Have range of values and different precision depending on the
used memory
ο‚§ Can behave abnormally in the calculations
What are Floating-Point Types?
14
ο‚§ Floating-point types are:
ο‚§ float (Β±1.5 Γ— 10βˆ’45 to Β±3.4 Γ— 1038)
ο‚§ 32-bits, precision of 7 digits
ο‚§ double (Β±5.0 Γ— 10βˆ’324 to Β±1.7 Γ— 10308)
ο‚§ 64-bits, precision of 15-16 digits
ο‚§ The default value of floating-point types:
ο‚§ Is 0.0F for the float type
ο‚§ Is 0.0D for the double type
Floating-Point Types
15
ο‚§ Difference in precision when using float and double:
ο‚§ NOTE: The β€œf” suffix in the first statement!
ο‚§ Real numbers are by default interpreted as double!
ο‚§ One should explicitly convert them to float
PI Precision – Example
float floatPI = 3.141592653589793238f;
double doublePI = 3.141592653589793238;
Console.WriteLine("Float PI is: {0}", floatPI);
Console.WriteLine("Double PI is: {0}", doublePI);
16
ο‚§ Sometimes abnormalities can be observed when using floating-
point numbers
ο‚§ Comparing floating-point numbers can not be performed directly
with the == operator
Abnormalities in the Floating-Point Calculations
double a = 1.0f;
double b = 0.33f;
double sum = 1.33f;
bool equal = (a+b == sum); // False!!!
Console.WriteLine("a+b={0} sum={1} equal={2}", a+b, sum, equal);
17
ο‚§ There is a special decimal floating-point real number type in C#:
ο‚§ decimal (Β±1,0 Γ— 10-28 to Β±7,9 Γ— 1028)
ο‚§ 128-bits, precision of 28-29 digits
ο‚§ Used for financial calculations
ο‚§ No round-off errors
ο‚§ Almost no loss of precision
ο‚§ The default value of decimal type is:
ο‚§ 0.0M (M is the suffix for decimal numbers)
Decimal Floating-Point Types
Floating-Point and Decimal
Floating-Point Types
Live Demo
Boolean Type
20
ο‚§ The Boolean data type:
ο‚§ Is declared by the bool keyword
ο‚§ Has two possible values: true and false
ο‚§ Is useful in logical expressions
ο‚§ The default value is false
The Boolean Data Type
21
ο‚§ Example of boolean variables taking values of true or false:
Boolean Values – Example
int a = 1;
int b = 2;
bool greaterAB = (a > b);
Console.WriteLine(greaterAB); // False
bool equalA1 = (a == 1);
Console.WriteLine(equalA1); // True
Boolean Type
Live Demo
Character Type
24
ο‚§ The character data type:
ο‚§ Represents symbolic information
ο‚§ Is declared by the char keyword
ο‚§ Gives each symbol a corresponding integer code
ο‚§ Has a '0' default value
ο‚§ Takes 16 bits of memory (from U+0000 to U+FFFF)
ο‚§ Holds a single Unicode character (or part of character)
The Character Data Type
25
ο‚§ The example below shows that every
character has an unique Unicode code:
Characters and Codes
char symbol = 'a';
Console.WriteLine("The code of '{0}' is: {1}", symbol, (int) symbol);
symbol = 'b';
Console.WriteLine("The code of '{0}' is: {1}", symbol, (int) symbol);
symbol = 'A';
Console.WriteLine("The code of '{0}' is: {1}", symbol, (int) symbol);
symbol = 'Ρ‰'; // Cyrillic letter 'sht'
Console.WriteLine("The code of '{0}' is: {1}", symbol, (int)symbol);
Character Type
Live Demo
String Type
28
ο‚§ The string data type:
ο‚§ Represents a sequence of characters
ο‚§ Is declared by the string keyword
ο‚§ Has a default value null (no value)
ο‚§ Strings are enclosed in quotes:
ο‚§ Strings can be concatenated
ο‚§ Using the + operator
The String Data Type
string s = "Hello, C#";
29
ο‚§ Concatenating the names of a person to obtain the full name:
ο‚§ We can concatenate strings and numbers as well:
Saying Hello – Example
string firstName = "Ivan";
string lastName = "Ivanov";
Console.WriteLine("Hello, {0}!", firstName);
string fullName = firstName + " " + lastName;
Console.WriteLine("Your full name is {0}.", fullName);
int age = 21;
Console.WriteLine("Hello, I am " + age + " years old");
String Type
Live Demo
Object Type
32
ο‚§ The object type:
ο‚§ Is declared by the object keyword
ο‚§ Is the base type of all other types
ο‚§ Can hold values of any type
The Object Type
object dataContainer = 5;
Console.Write("The value of dataContainer is: ");
Console.WriteLine(dataContainer);
dataContainer = "Five";
Console.Write("The value of dataContainer is: ");
Console.WriteLine(dataContainer);
Objects
Live Demo
Introducing Variables
ο‚§ Variables keep data in the computer memory
ο‚§ A variable is a:
ο‚§ Placeholder of information that can be changed at run-time
ο‚§ Variables allow you to:
ο‚§ Store information
ο‚§ Retrieve the stored information
ο‚§ Change the stored information
35
What Is a Variable?
36
ο‚§ A variable has:
ο‚§ Name
ο‚§ Type (of stored data)
ο‚§ Value
ο‚§ Example:
ο‚§ Name: counter
ο‚§ Type: int
ο‚§ Value: 5
Variable Characteristics
int counter = 5;
Declaring and Using Variables
38
ο‚§ When declaring a variable we:
ο‚§ Specify its type
ο‚§ Specify its name (called identifier)
ο‚§ May give it an initial value
ο‚§ The syntax is the following:
ο‚§ Example:
Declaring Variables
<data_type> <identifier> [= <initialization>];
int height = 200;
39
ο‚§ Identifiers may consist of:
ο‚§ Letters (Unicode)
ο‚§ Digits [0-9]
ο‚§ Underscore "_"
ο‚§ Examples: count, firstName, Page
ο‚§ Identifiers
ο‚§ Can begin only with a letter or an underscore
ο‚§ Cannot be a C# keyword (like int and class)
Identifiers
40
ο‚§ Identifiers
ο‚§ Should have a descriptive name
ο‚§ E.g. firstName, not dasfas or p17
ο‚§ It is recommended to use only Latin letters
ο‚§ Should be neither too long nor too short
ο‚§ Note:
ο‚§ In C# small letters are considered different
than the capital letters (case sensitivity)
Identifiers (2)
41
ο‚§ Examples of syntactically correct identifiers:
ο‚§ Examples of syntactically incorrect identifiers:
Identifiers – Examples
int new; // new is a keyword
int 2Pac; // cannot begin with a digit
int New = 2; // here N is capital
int _2Pac; // this identifiers begins with underscore _
string ΠΏΠΎΠ·Π΄Ρ€Π°Π² = "Hello"; // Unicode symbols are acceptable
string greeting = "Hello"; // more appropriate name
int n = 100; // undescriptive
int numberOfClients = 100; // good, descriptive name
int numberOfPrivateClientOfTheFirm = 100; // overdescriptive
Assigning Values
To Variables
43
ο‚§ Assigning values to variables
ο‚§ Use the = operator
ο‚§ The = operator
ο‚§ Holds a variable identifier on the left
ο‚§ Value of the corresponding data type on the right
ο‚§ Or expression of compatible type
ο‚§ Could be used in a cascade calling
ο‚§ Where assigning is done from right to left
Assigning Values
44
Assigning Values – Examples
int firstValue = 5;
int secondValue;
int thirdValue;
// Using an already declared variable:
secondValue = firstValue;
// The following cascade calling assigns
// 3 to firstValue and then firstValue
// to thirdValue, so both variables have
// the value 3 as a result:
thirdValue = firstValue = 3; // Avoid cascading assignments!
45
ο‚§ Initializing
ο‚§ Means "to assign an initial value"
ο‚§ Must be done before the variable is used!
ο‚§ Several ways of initializing:
ο‚§ By using the new keyword
ο‚§ By using a literal expression
ο‚§ By referring to an already initialized variable
Initializing Variables
46
ο‚§ Example of variable initializations:
Initialization – Examples
// The following would assign the default
// value of the int type to num:
int num = new int(); // num = 0
// This is how we use a literal expression:
float heightInMeters = 1.74f;
// Here we use an already initialized variable:
string greeting = "Hello World!";
string message = greeting;
Assigning and
Initializing Variables
Live Demo
Literals
49
ο‚§ Literals are:
ο‚§ Representations of values in the source code
ο‚§ There are several types of literals
ο‚§ Boolean
ο‚§ Integer
ο‚§ Real
ο‚§ Character
ο‚§ String
ο‚§ The null literal
What are Literals?
50
ο‚§ The boolean literals are:
ο‚§ true
ο‚§ false
ο‚§ The integer literals:
ο‚§ Are used for variables of type int, uint, long, and ulong
ο‚§ Consist of digits
ο‚§ May have a sign (+,-)
ο‚§ May be in a hexadecimal format
Boolean and Integer Literals
51
ο‚§ 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 or ulong
ο‚§ E.g. 9876543L, 0L
Integer Literals
52
ο‚§ Note: the letter β€˜l’ is easily confused with the digit β€˜1’
ο‚§ So it’s better to use β€˜L’
Integer Literals – Examples
// The following variables are initialized with the same value:
int numberInHex = -0x10;
int numberInDec = -16;
// The following causes an error, because 234u is of type uint
int unsignedInt = 234u;
// The following causes an error, because 234L is of type long
int longInt = 234L;
53
ο‚§ The real literals:
ο‚§ Are used for values of type float, double and decimal
ο‚§ May consist of digits, a sign and β€œ.”
ο‚§ May be in exponential notation: 6.02e+23
ο‚§ The β€œf” and β€œF” suffixes mean float
ο‚§ The β€œd” and β€œD” suffixes mean double
ο‚§ The β€œm” and β€œM” suffixes mean decimal
ο‚§ The default interpretation is double
Real Literals
54
ο‚§ Example of incorrect float literal:
ο‚§ A correct way to assign a floating-point value
ο‚§ Exponential format for assigning float values:
Real Literals – Example
// The following causes an error because 12.5 is double by default
float realNumber = 12.5;
// The following is the correct way of assigning the value:
float realNumber = 12.5f;
// This is the same value in exponential format:
float realNumber = 6.02e+23;
float realNumber = 1.25e-7f;
55
ο‚§ The character literals:
ο‚§ Are used for values of the char type
ο‚§ Consist of two single quotes surrounding the character value:
ο‚§ The value may be:
ο‚§ Symbol
ο‚§ The code of the symbol
ο‚§ Escaping sequence
Character Literals
'<value>'
56
ο‚§ Escaping sequences are:
ο‚§ Means of presenting a symbol that is usually interpreted
otherwise (like ')
ο‚§ Means of presenting system symbols (like the new line symbol)
ο‚§ Common escaping sequences are:
ο‚§ ' for single quote " for double quote
ο‚§  for backslash n for new line
ο‚§ uXXXX for denoting any other Unicode symbol
Escaping Sequences
57
ο‚§ Examples of different character literals:
Character Literals – Example
char symbol = 'a'; // An ordinary symbol
symbol = 'u006F'; // Unicode symbol code in a
// hexadecimal format (letter 'o')
symbol = 'u8449'; // 葉 (Leaf in Traditional Chinese)
symbol = '''; // Assigning the single quote symbol
symbol = ''; // Assigning the backslash symbol
symbol = 'n'; // Assigning new line symbol
symbol = 't'; // Assigning TAB symbol
symbol = "a"; // Incorrect: use single quotes
58
ο‚§ String literals:
ο‚§ Are used for values of the string type
ο‚§ Consist of two double quotes surrounding the value: "<value>"
ο‚§ The value is a sequence of character literals
ο‚§ May have a @ prefix which ignores the used escaping sequences:
@"<value>"
String Literals
string s = "I am a sting literal";
string s = @"C:WINDOWSSystem32driversbeep.sys";
59
ο‚§ Benefits of quoted strings (with the @ prefix):
ο‚§ In quoted strings "" is used instead of "!
String Literals – Examples
// Here is a string literal using escape sequences
string quotation = ""Hello, Jude", he said.";
string path = "C:Windowsnotepad.exe";
// Here is an example of the usage of @
quotation = @"""Hello, Jimmy!"", she answered.";
path = @"C:Windowsnotepad.exe";
string str = @"some
text";
String Literals
Live Demo
Nullable Types
61
62
ο‚§ Nullable types are instances of the System.Nullable
structure
ο‚§ Wrapper around the primitive types
ο‚§ E.g. int?, double?, etc.
ο‚§ Nullabe type can represent the normal range of values for its
underlying value type, plus an additional null value
ο‚§ Useful when dealing with databases or other structures that
have default value null
Nullable Types
ο‚§ Example with int:
ο‚§ Example with double:
Nullable Types – Example
int? someInteger = null;
Console.WriteLine("This is the integer with Null value -> " + someInteger);
someInteger = 5;
Console.WriteLine("This is the integer with value 5 -> " + someInteger);
double? someDouble = null;
Console.WriteLine(
"This is the real number with Null value -> " + someDouble);
someDouble = 2.5;
Console.WriteLine("This is the real number with value 5 -> " + someDouble);
63
Nullable Types
Live Demo
65
ο‚§ Data types are domains of possible values
ο‚§ E.g. number, character, date, string
ο‚§ Integer types hold whole numbers
ο‚§ E.g. 5, -2, 32768
ο‚§ Float and double hold floating-point numbers
ο‚§ E.g. 3.14159206, 6.02e+23
ο‚§ Decimal type holds money and financial information, e.g. 12.80
ο‚§ Boolean type holds true or false
Summary
66
ο‚§ Character type holds a single Unicode character
ο‚§ E.g. 'A', 'n', '0', '€', 'u0AF4'
ο‚§ String type hold a text, e.g. "Hello C#"
ο‚§ Object type hold any value
ο‚§ E.g. string, number, character, date, …
ο‚§ Variables are named pieces of memory that hold a value
ο‚§ Identifiers are the names of variables, classes, methods, etc.
ο‚§ Literals are the values of the primitive types, e.g. 0xFE, 'uF7B3'
ο‚§ Nullable types can hold a value or null (absence of value)
Summary (2)
?
http://softuni.bg/courses/csharp-basics/
Primitive Data Types and Variables
License
ο‚§ This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-
NonCommercial-ShareAlike 4.0 International" license
68
ο‚§ Attribution: this work may contain portions from
ο‚§ "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license
ο‚§ "C# Part I" course by Telerik Academy under CC-BY-NC-SA license
Free Trainings @ Software University
ο‚§ Software University Foundation – softuni.org
ο‚§ Software University – High-Quality Education,
Profession and Job for Software Developers
ο‚§ softuni.bg
ο‚§ Software University @ Facebook
ο‚§ facebook.com/SoftwareUniversity
ο‚§ Software University @ YouTube
ο‚§ youtube.com/SoftwareUniversity
ο‚§ Software University Forums – forum.softuni.bg

More Related Content

PPTX
03. Operators Expressions and statements
PPTX
06.Loops
PPTX
01. Introduction to Programming
PPTX
04. Console Input Output
PPTX
07. Arrays
PPTX
05. Conditional Statements
PPT
Java Basics
03. Operators Expressions and statements
06.Loops
01. Introduction to Programming
04. Console Input Output
07. Arrays
05. Conditional Statements
Java Basics

What's hot (20)

PDF
C Recursion, Pointers, Dynamic memory management
PPTX
Control flow statements in java
PPT
structure and union
PPT
Functions in C++
PDF
Functional Design Patterns (DevTernity 2018)
PPTX
Pointer in c program
PPTX
Function in c
PPT
Collection v3
PPTX
Switch statement, break statement, go to statement
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
PPT
JAVA OOP
PPT
Functions in C++
PPTX
Recursive Function
PDF
C Programming Storage classes, Recursion
PPT
While loop
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPT
JAVA Variables and Operators
PPTX
Strings in c++
PPT
Log4 J
PPTX
Autoboxing And Unboxing In Java
C Recursion, Pointers, Dynamic memory management
Control flow statements in java
structure and union
Functions in C++
Functional Design Patterns (DevTernity 2018)
Pointer in c program
Function in c
Collection v3
Switch statement, break statement, go to statement
Java Foundations: Basic Syntax, Conditions, Loops
JAVA OOP
Functions in C++
Recursive Function
C Programming Storage classes, Recursion
While loop
CONDITIONAL STATEMENT IN C LANGUAGE
JAVA Variables and Operators
Strings in c++
Log4 J
Autoboxing And Unboxing In Java
Ad

Similar to 02. Primitive Data Types and Variables (20)

PPT
02 Primitive data types and variables
PPTX
CSharp Language Overview Part 1
PPTX
C# overview part 1
PPT
Primitive Data Types and Variables Lesson 02
PPTX
Lesson 4 Basic Programming Constructs.pptx
PPTX
JAVA LESSON-01.pptx
PPT
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
PPTX
PROGRAMMING IN C - Inroduction.pptx
PPTX
unit 1 (1).pptx
PPTX
data types in C-Sharp (C#)
PPTX
2. Variables and Data Types in C++ proramming.pptx
PPT
5-Lec - Datatypes.ppt
PDF
C# Language Overview Part I
PPT
02. Data Type and Variables
PPT
Csharp4 basics
PPT
02a fundamental c++ types, arithmetic
PPTX
Module 1:Introduction
PDF
POLITEKNIK MALAYSIA
PPTX
02 Primitive data types and variables
CSharp Language Overview Part 1
C# overview part 1
Primitive Data Types and Variables Lesson 02
Lesson 4 Basic Programming Constructs.pptx
JAVA LESSON-01.pptx
CPLUSPLUS UET PESHAWAR BS ELECTRICAL 2ND SEMSTER Lecture02.ppt
03 and 04 .Operators, Expressions, working with the console and conditional s...
PROGRAMMING IN C - Inroduction.pptx
unit 1 (1).pptx
data types in C-Sharp (C#)
2. Variables and Data Types in C++ proramming.pptx
5-Lec - Datatypes.ppt
C# Language Overview Part I
02. Data Type and Variables
Csharp4 basics
02a fundamental c++ types, arithmetic
Module 1:Introduction
POLITEKNIK MALAYSIA
Ad

More from Intro C# Book (20)

PPTX
17. Java data structures trees representation and traversal
PPTX
Java Problem solving
PPTX
21. Java High Quality Programming Code
PPTX
20.5 Java polymorphism
PPTX
20.4 Java interfaces and abstraction
PPTX
20.3 Java encapsulation
PPTX
20.2 Java inheritance
PPTX
20.1 Java working with abstraction
PPTX
19. Java data structures algorithms and complexity
PPTX
18. Java associative arrays
PPTX
16. Java stacks and queues
PPTX
14. Java defining classes
PPTX
13. Java text processing
PPTX
12. Java Exceptions and error handling
PPTX
11. Java Objects and classes
PPTX
09. Java Methods
PPTX
05. Java Loops Methods and Classes
PPTX
07. Java Array, Set and Maps
PPTX
02. Data Types and variables
PPTX
01. Introduction to programming with java
17. Java data structures trees representation and traversal
Java Problem solving
21. Java High Quality Programming Code
20.5 Java polymorphism
20.4 Java interfaces and abstraction
20.3 Java encapsulation
20.2 Java inheritance
20.1 Java working with abstraction
19. Java data structures algorithms and complexity
18. Java associative arrays
16. Java stacks and queues
14. Java defining classes
13. Java text processing
12. Java Exceptions and error handling
11. Java Objects and classes
09. Java Methods
05. Java Loops Methods and Classes
07. Java Array, Set and Maps
02. Data Types and variables
01. Introduction to programming with java

Recently uploaded (20)

PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
Testing WebRTC applications at scale.pdf
PPTX
Funds Management Learning Material for Beg
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PPTX
artificial intelligence overview of it and more
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
innovation process that make everything different.pptx
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
Introuction about WHO-FIC in ICD-10.pptx
WebRTC in SignalWire - troubleshooting media negotiation
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
Paper PDF World Game (s) Great Redesign.pdf
Testing WebRTC applications at scale.pdf
Funds Management Learning Material for Beg
Design_with_Watersergyerge45hrbgre4top (1).ppt
Decoding a Decade: 10 Years of Applied CTI Discipline
Slides PPTX World Game (s) Eco Economic Epochs.pptx
artificial intelligence overview of it and more
SASE Traffic Flow - ZTNA Connector-1.pdf
Cloud-Scale Log Monitoring _ Datadog.pdf
Job_Card_System_Styled_lorem_ipsum_.pptx
INTERNET------BASICS-------UPDATED PPT PRESENTATION
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
presentation_pfe-universite-molay-seltan.pptx
innovation process that make everything different.pptx
QR Codes Qr codecodecodecodecocodedecodecode
Unit-1 introduction to cyber security discuss about how to secure a system

02. Primitive Data Types and Variables

  • 1. Primitive Data Types and Variables Integer, Floating-Point, Text Data, Variables, Literals Svetlin Nakov Technical Trainer www.nakov.com Software University http://softuni.bg
  • 2. Table of Contents 1. Primitive Data Types ο‚§ Integer ο‚§ Floating-Point / Decimal Floating-Point ο‚§ Boolean ο‚§ Character ο‚§ String ο‚§ Object 2. Declaring and Using Variables ο‚§ Identifiers, Variables, Literals 3. Nullable Types 2
  • 4. 4 ο‚§ Computers are machines that process data ο‚§ Data is stored in the computer memory in variables ο‚§ Variables have name, data type and value ο‚§ Example of variable definition and assignment in C# ο‚§ When processed, data is stored back into variables How Computing Works? int count = 5;Data type Variable name Variable value
  • 5. 5 ο‚§ A data type: ο‚§ Is a domain of values of similar characteristics ο‚§ Defines the type of information stored in the computer memory (in a variable) ο‚§ Examples: ο‚§ Positive integers: 1, 2, 3, … ο‚§ Alphabetical characters: a, b, c, … ο‚§ Days of week: Monday, Tuesday, … What Is a Data Type?
  • 6. 6 ο‚§ 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
  • 8. 8 ο‚§ Integer types: ο‚§ Represent whole numbers ο‚§ May be signed or unsigned ο‚§ Have range of values, depending on the size of memory used ο‚§ The default value of integer types is: ο‚§ 0 – for integer types, except ο‚§ 0L – for the long type What are Integer Types?
  • 9. 9 ο‚§ sbyte (-128 to 127): signed 8-bit ο‚§ byte (0 to 255): unsigned 8-bit ο‚§ short (-32,768 to 32,767): signed 16-bit ο‚§ ushort (0 to 65,535): unsigned 16-bit ο‚§ int (-2,147,483,648 to 2,147,483,647): signed 32-bit ο‚§ uint (0 to 4,294,967,295): unsigned 32-bit ο‚§ long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807): signed 64-bit ο‚§ ulong (0 to 18,446,744,073,709,551,615): unsigned 64-bit Integer Types
  • 10. 10 ο‚§ Depending on the unit of measure we may use different data types: Measuring Time – Example byte centuries = 20; // A small number (up to 255) ushort years = 2000; // A small number (up to 32767) uint days = 730480; // A large number (up to 4.3 billions) ulong hours = 17531520; // A very big number (up to 18.4*10^18) Console.WriteLine( "{0} centuries is {1} years, or {2} days, or {3} hours.", centuries, years, days, hours);
  • 13. 13 ο‚§ Floating-point types: ο‚§ Represent real numbers ο‚§ May be signed or unsigned ο‚§ Have range of values and different precision depending on the used memory ο‚§ Can behave abnormally in the calculations What are Floating-Point Types?
  • 14. 14 ο‚§ Floating-point types are: ο‚§ float (Β±1.5 Γ— 10βˆ’45 to Β±3.4 Γ— 1038) ο‚§ 32-bits, precision of 7 digits ο‚§ double (Β±5.0 Γ— 10βˆ’324 to Β±1.7 Γ— 10308) ο‚§ 64-bits, precision of 15-16 digits ο‚§ The default value of floating-point types: ο‚§ Is 0.0F for the float type ο‚§ Is 0.0D for the double type Floating-Point Types
  • 15. 15 ο‚§ Difference in precision when using float and double: ο‚§ NOTE: The β€œf” suffix in the first statement! ο‚§ Real numbers are by default interpreted as double! ο‚§ One should explicitly convert them to float PI Precision – Example float floatPI = 3.141592653589793238f; double doublePI = 3.141592653589793238; Console.WriteLine("Float PI is: {0}", floatPI); Console.WriteLine("Double PI is: {0}", doublePI);
  • 16. 16 ο‚§ Sometimes abnormalities can be observed when using floating- point numbers ο‚§ Comparing floating-point numbers can not be performed directly with the == operator Abnormalities in the Floating-Point Calculations double a = 1.0f; double b = 0.33f; double sum = 1.33f; bool equal = (a+b == sum); // False!!! Console.WriteLine("a+b={0} sum={1} equal={2}", a+b, sum, equal);
  • 17. 17 ο‚§ There is a special decimal floating-point real number type in C#: ο‚§ decimal (Β±1,0 Γ— 10-28 to Β±7,9 Γ— 1028) ο‚§ 128-bits, precision of 28-29 digits ο‚§ Used for financial calculations ο‚§ No round-off errors ο‚§ Almost no loss of precision ο‚§ The default value of decimal type is: ο‚§ 0.0M (M is the suffix for decimal numbers) Decimal Floating-Point Types
  • 20. 20 ο‚§ The Boolean data type: ο‚§ Is declared by the bool keyword ο‚§ Has two possible values: true and false ο‚§ Is useful in logical expressions ο‚§ The default value is false The Boolean Data Type
  • 21. 21 ο‚§ Example of boolean variables taking values of true or false: Boolean Values – Example int a = 1; int b = 2; bool greaterAB = (a > b); Console.WriteLine(greaterAB); // False bool equalA1 = (a == 1); Console.WriteLine(equalA1); // True
  • 24. 24 ο‚§ The character data type: ο‚§ Represents symbolic information ο‚§ Is declared by the char keyword ο‚§ Gives each symbol a corresponding integer code ο‚§ Has a '0' default value ο‚§ Takes 16 bits of memory (from U+0000 to U+FFFF) ο‚§ Holds a single Unicode character (or part of character) The Character Data Type
  • 25. 25 ο‚§ The example below shows that every character has an unique Unicode code: Characters and Codes char symbol = 'a'; Console.WriteLine("The code of '{0}' is: {1}", symbol, (int) symbol); symbol = 'b'; Console.WriteLine("The code of '{0}' is: {1}", symbol, (int) symbol); symbol = 'A'; Console.WriteLine("The code of '{0}' is: {1}", symbol, (int) symbol); symbol = 'Ρ‰'; // Cyrillic letter 'sht' Console.WriteLine("The code of '{0}' is: {1}", symbol, (int)symbol);
  • 28. 28 ο‚§ The string data type: ο‚§ Represents a sequence of characters ο‚§ Is declared by the string keyword ο‚§ Has a default value null (no value) ο‚§ Strings are enclosed in quotes: ο‚§ Strings can be concatenated ο‚§ Using the + operator The String Data Type string s = "Hello, C#";
  • 29. 29 ο‚§ Concatenating the names of a person to obtain the full name: ο‚§ We can concatenate strings and numbers as well: Saying Hello – Example string firstName = "Ivan"; string lastName = "Ivanov"; Console.WriteLine("Hello, {0}!", firstName); string fullName = firstName + " " + lastName; Console.WriteLine("Your full name is {0}.", fullName); int age = 21; Console.WriteLine("Hello, I am " + age + " years old");
  • 32. 32 ο‚§ The object type: ο‚§ Is declared by the object keyword ο‚§ Is the base type of all other types ο‚§ Can hold values of any type The Object Type object dataContainer = 5; Console.Write("The value of dataContainer is: "); Console.WriteLine(dataContainer); dataContainer = "Five"; Console.Write("The value of dataContainer is: "); Console.WriteLine(dataContainer);
  • 35. ο‚§ Variables keep data in the computer memory ο‚§ A variable is a: ο‚§ Placeholder of information that can be changed at run-time ο‚§ Variables allow you to: ο‚§ Store information ο‚§ Retrieve the stored information ο‚§ Change the stored information 35 What Is a Variable?
  • 36. 36 ο‚§ A variable has: ο‚§ Name ο‚§ Type (of stored data) ο‚§ Value ο‚§ Example: ο‚§ Name: counter ο‚§ Type: int ο‚§ Value: 5 Variable Characteristics int counter = 5;
  • 37. Declaring and Using Variables
  • 38. 38 ο‚§ When declaring a variable we: ο‚§ Specify its type ο‚§ Specify its name (called identifier) ο‚§ May give it an initial value ο‚§ The syntax is the following: ο‚§ Example: Declaring Variables <data_type> <identifier> [= <initialization>]; int height = 200;
  • 39. 39 ο‚§ Identifiers may consist of: ο‚§ Letters (Unicode) ο‚§ Digits [0-9] ο‚§ Underscore "_" ο‚§ Examples: count, firstName, Page ο‚§ Identifiers ο‚§ Can begin only with a letter or an underscore ο‚§ Cannot be a C# keyword (like int and class) Identifiers
  • 40. 40 ο‚§ Identifiers ο‚§ Should have a descriptive name ο‚§ E.g. firstName, not dasfas or p17 ο‚§ It is recommended to use only Latin letters ο‚§ Should be neither too long nor too short ο‚§ Note: ο‚§ In C# small letters are considered different than the capital letters (case sensitivity) Identifiers (2)
  • 41. 41 ο‚§ Examples of syntactically correct identifiers: ο‚§ Examples of syntactically incorrect identifiers: Identifiers – Examples int new; // new is a keyword int 2Pac; // cannot begin with a digit int New = 2; // here N is capital int _2Pac; // this identifiers begins with underscore _ string ΠΏΠΎΠ·Π΄Ρ€Π°Π² = "Hello"; // Unicode symbols are acceptable string greeting = "Hello"; // more appropriate name int n = 100; // undescriptive int numberOfClients = 100; // good, descriptive name int numberOfPrivateClientOfTheFirm = 100; // overdescriptive
  • 43. 43 ο‚§ Assigning values to variables ο‚§ Use the = operator ο‚§ The = operator ο‚§ Holds a variable identifier on the left ο‚§ Value of the corresponding data type on the right ο‚§ Or expression of compatible type ο‚§ Could be used in a cascade calling ο‚§ Where assigning is done from right to left Assigning Values
  • 44. 44 Assigning Values – Examples int firstValue = 5; int secondValue; int thirdValue; // Using an already declared variable: secondValue = firstValue; // The following cascade calling assigns // 3 to firstValue and then firstValue // to thirdValue, so both variables have // the value 3 as a result: thirdValue = firstValue = 3; // Avoid cascading assignments!
  • 45. 45 ο‚§ Initializing ο‚§ Means "to assign an initial value" ο‚§ Must be done before the variable is used! ο‚§ Several ways of initializing: ο‚§ By using the new keyword ο‚§ By using a literal expression ο‚§ By referring to an already initialized variable Initializing Variables
  • 46. 46 ο‚§ Example of variable initializations: Initialization – Examples // The following would assign the default // value of the int type to num: int num = new int(); // num = 0 // This is how we use a literal expression: float heightInMeters = 1.74f; // Here we use an already initialized variable: string greeting = "Hello World!"; string message = greeting;
  • 49. 49 ο‚§ Literals are: ο‚§ Representations of values in the source code ο‚§ There are several types of literals ο‚§ Boolean ο‚§ Integer ο‚§ Real ο‚§ Character ο‚§ String ο‚§ The null literal What are Literals?
  • 50. 50 ο‚§ The boolean literals are: ο‚§ true ο‚§ false ο‚§ The integer literals: ο‚§ Are used for variables of type int, uint, long, and ulong ο‚§ Consist of digits ο‚§ May have a sign (+,-) ο‚§ May be in a hexadecimal format Boolean and Integer Literals
  • 51. 51 ο‚§ 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 or ulong ο‚§ E.g. 9876543L, 0L Integer Literals
  • 52. 52 ο‚§ Note: the letter β€˜l’ is easily confused with the digit β€˜1’ ο‚§ So it’s better to use β€˜L’ Integer Literals – Examples // The following variables are initialized with the same value: int numberInHex = -0x10; int numberInDec = -16; // The following causes an error, because 234u is of type uint int unsignedInt = 234u; // The following causes an error, because 234L is of type long int longInt = 234L;
  • 53. 53 ο‚§ The real literals: ο‚§ Are used for values of type float, double and decimal ο‚§ May consist of digits, a sign and β€œ.” ο‚§ May be in exponential notation: 6.02e+23 ο‚§ The β€œf” and β€œF” suffixes mean float ο‚§ The β€œd” and β€œD” suffixes mean double ο‚§ The β€œm” and β€œM” suffixes mean decimal ο‚§ The default interpretation is double Real Literals
  • 54. 54 ο‚§ Example of incorrect float literal: ο‚§ A correct way to assign a floating-point value ο‚§ Exponential format for assigning float values: Real Literals – Example // The following causes an error because 12.5 is double by default float realNumber = 12.5; // The following is the correct way of assigning the value: float realNumber = 12.5f; // This is the same value in exponential format: float realNumber = 6.02e+23; float realNumber = 1.25e-7f;
  • 55. 55 ο‚§ The character literals: ο‚§ Are used for values of the char type ο‚§ Consist of two single quotes surrounding the character value: ο‚§ The value may be: ο‚§ Symbol ο‚§ The code of the symbol ο‚§ Escaping sequence Character Literals '<value>'
  • 56. 56 ο‚§ Escaping sequences are: ο‚§ Means of presenting a symbol that is usually interpreted otherwise (like ') ο‚§ Means of presenting system symbols (like the new line symbol) ο‚§ Common escaping sequences are: ο‚§ ' for single quote " for double quote ο‚§ for backslash n for new line ο‚§ uXXXX for denoting any other Unicode symbol Escaping Sequences
  • 57. 57 ο‚§ Examples of different character literals: Character Literals – Example char symbol = 'a'; // An ordinary symbol symbol = 'u006F'; // Unicode symbol code in a // hexadecimal format (letter 'o') symbol = 'u8449'; // 葉 (Leaf in Traditional Chinese) symbol = '''; // Assigning the single quote symbol symbol = ''; // Assigning the backslash symbol symbol = 'n'; // Assigning new line symbol symbol = 't'; // Assigning TAB symbol symbol = "a"; // Incorrect: use single quotes
  • 58. 58 ο‚§ String literals: ο‚§ Are used for values of the string type ο‚§ Consist of two double quotes surrounding the value: "<value>" ο‚§ The value is a sequence of character literals ο‚§ May have a @ prefix which ignores the used escaping sequences: @"<value>" String Literals string s = "I am a sting literal"; string s = @"C:WINDOWSSystem32driversbeep.sys";
  • 59. 59 ο‚§ Benefits of quoted strings (with the @ prefix): ο‚§ In quoted strings "" is used instead of "! String Literals – Examples // Here is a string literal using escape sequences string quotation = ""Hello, Jude", he said."; string path = "C:Windowsnotepad.exe"; // Here is an example of the usage of @ quotation = @"""Hello, Jimmy!"", she answered."; path = @"C:Windowsnotepad.exe"; string str = @"some text";
  • 62. 62 ο‚§ Nullable types are instances of the System.Nullable structure ο‚§ Wrapper around the primitive types ο‚§ E.g. int?, double?, etc. ο‚§ Nullabe type can represent the normal range of values for its underlying value type, plus an additional null value ο‚§ Useful when dealing with databases or other structures that have default value null Nullable Types
  • 63. ο‚§ Example with int: ο‚§ Example with double: Nullable Types – Example int? someInteger = null; Console.WriteLine("This is the integer with Null value -> " + someInteger); someInteger = 5; Console.WriteLine("This is the integer with value 5 -> " + someInteger); double? someDouble = null; Console.WriteLine( "This is the real number with Null value -> " + someDouble); someDouble = 2.5; Console.WriteLine("This is the real number with value 5 -> " + someDouble); 63
  • 65. 65 ο‚§ Data types are domains of possible values ο‚§ E.g. number, character, date, string ο‚§ Integer types hold whole numbers ο‚§ E.g. 5, -2, 32768 ο‚§ Float and double hold floating-point numbers ο‚§ E.g. 3.14159206, 6.02e+23 ο‚§ Decimal type holds money and financial information, e.g. 12.80 ο‚§ Boolean type holds true or false Summary
  • 66. 66 ο‚§ Character type holds a single Unicode character ο‚§ E.g. 'A', 'n', '0', '€', 'u0AF4' ο‚§ String type hold a text, e.g. "Hello C#" ο‚§ Object type hold any value ο‚§ E.g. string, number, character, date, … ο‚§ Variables are named pieces of memory that hold a value ο‚§ Identifiers are the names of variables, classes, methods, etc. ο‚§ Literals are the values of the primitive types, e.g. 0xFE, 'uF7B3' ο‚§ Nullable types can hold a value or null (absence of value) Summary (2)
  • 68. License ο‚§ This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license 68 ο‚§ Attribution: this work may contain portions from ο‚§ "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license ο‚§ "C# Part I" course by Telerik Academy under CC-BY-NC-SA license
  • 69. Free Trainings @ Software University ο‚§ Software University Foundation – softuni.org ο‚§ Software University – High-Quality Education, Profession and Job for Software Developers ο‚§ softuni.bg ο‚§ Software University @ Facebook ο‚§ facebook.com/SoftwareUniversity ο‚§ Software University @ YouTube ο‚§ youtube.com/SoftwareUniversity ο‚§ Software University Forums – forum.softuni.bg

Editor's Notes

  • #4: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #8: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #13: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #20: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #24: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #28: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #32: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #35: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #38: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #39: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #43: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #49: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*