SlideShare uma empresa Scribd logo
1 de 69
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

Mais conteúdo relacionado

Mais procurados (20)

Strings in c language
Strings in  c languageStrings in  c language
Strings in c language
 
Array
ArrayArray
Array
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
cs8251 unit 1 ppt
cs8251 unit 1 pptcs8251 unit 1 ppt
cs8251 unit 1 ppt
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Loops
LoopsLoops
Loops
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
enums
enumsenums
enums
 
Data types in java
Data types in javaData types in java
Data types in java
 
Data types
Data typesData types
Data types
 
System testing ppt
System testing pptSystem testing ppt
System testing ppt
 
Multiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchartMultiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchart
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Operators and expression in c#
Operators and expression in c#Operators and expression in c#
Operators and expression in c#
 
Data types
Data typesData types
Data types
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 

Semelhante a 02. Primitive Data Types and Variables

02 Primitive data types and variables
02 Primitive data types and variables02 Primitive data types and variables
02 Primitive data types and variablesmaznabili
 
CSharp Language Overview Part 1
CSharp Language Overview Part 1CSharp Language Overview Part 1
CSharp Language Overview Part 1Hossein Zahed
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxNithya K
 
data types in C-Sharp (C#)
data types in C-Sharp (C#)data types in C-Sharp (C#)
data types in C-Sharp (C#)Abid Kohistani
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.pptAqeelAbbas94
 
C# Language Overview Part I
C# Language Overview Part IC# Language Overview Part I
C# Language Overview Part IDoncho Minkov
 
02. Data Type and Variables
02. Data Type and Variables02. Data Type and Variables
02. Data Type and VariablesTommy Vercety
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introductionnikshaikh786
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingSherwin Banaag Sapin
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docxPinkiVats1
 

Semelhante a 02. Primitive Data Types and Variables (20)

02 Primitive data types and variables
02 Primitive data types and variables02 Primitive data types and variables
02 Primitive data types and variables
 
CSharp Language Overview Part 1
CSharp Language Overview Part 1CSharp Language Overview Part 1
CSharp Language Overview Part 1
 
C# overview part 1
C# overview part 1C# overview part 1
C# overview part 1
 
Primitive Data Types and Variables Lesson 02
Primitive Data Types and Variables Lesson 02Primitive Data Types and Variables Lesson 02
Primitive Data Types and Variables Lesson 02
 
JAVA LESSON-01.pptx
JAVA LESSON-01.pptxJAVA LESSON-01.pptx
JAVA LESSON-01.pptx
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptx
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
data types in C-Sharp (C#)
data types in C-Sharp (C#)data types in C-Sharp (C#)
data types in C-Sharp (C#)
 
5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt5-Lec - Datatypes.ppt
5-Lec - Datatypes.ppt
 
C# Language Overview Part I
C# Language Overview Part IC# Language Overview Part I
C# Language Overview Part I
 
02. Data Type and Variables
02. Data Type and Variables02. Data Type and Variables
02. Data Type and Variables
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C#
C#C#
C#
 
Data Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# ProgrammingData Types, Variables, and Constants in C# Programming
Data Types, Variables, and Constants in C# Programming
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docx
 
C# basics
C# basicsC# basics
C# basics
 

Mais de Intro C# Book

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
 
Java Problem solving
Java Problem solving Java Problem solving
Java Problem solving Intro C# Book
 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming CodeIntro C# Book
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism Intro C# Book
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulationIntro C# Book
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritanceIntro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexityIntro C# Book
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arraysIntro C# Book
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classesIntro C# Book
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processingIntro C# Book
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handlingIntro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro C# Book
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variablesIntro C# Book
 
01. Introduction to programming with java
01. Introduction to programming with java01. Introduction to programming with java
01. Introduction to programming with javaIntro C# Book
 

Mais de Intro C# Book (20)

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
 
Java Problem solving
Java Problem solving Java Problem solving
Java Problem solving
 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming Code
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
01. Introduction to programming with java
01. Introduction to programming with java01. Introduction to programming with java
01. Introduction to programming with java
 

Último

20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdfMatthew Sinclair
 
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...SUHANI PANDEY
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...SUHANI PANDEY
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...nilamkumrai
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...SUHANI PANDEY
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445ruhi
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...SUHANI PANDEY
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"growthgrids
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceDelhi Call girls
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...tanu pandey
 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...SUHANI PANDEY
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...SUHANI PANDEY
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)Delhi Call girls
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLimonikaupta
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...roncy bisnoi
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdfMatthew Sinclair
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirtrahman018755
 
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...SUHANI PANDEY
 

Último (20)

20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
Wadgaon Sheri $ Call Girls Pune 10k @ I'm VIP Independent Escorts Girls 80057...
 

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

Notas do Editor

  1. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  3. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  4. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  5. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  6. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  7. (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.*
  9. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  10. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  11. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  12. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*