Nikolay Kostov (Nikolay.IT)
Topics covered:
- Primitive Data Types
- Integer
- Floating-Point / Decimal Floating-Point
- Boolean
- Character
- String
- Object
- Declaring and Using Variables
- Identifiers
- Declaring Variables and Assigning Values
- Literals
Video (in Bulgarian)
Presentation Content
How Computing Works?
- 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#
What Is a Data Type?
- 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 , …
Data Type Characteristics
- A data type has:
- Name (C#keyword or .NET type)
- Size (how much memory is used)
- Defaultvalue
- Example:
- Integer numbers in C#
- Name: int
- Size:32bits (4bytes)
- Default value: 0
What are Integer Types?
- Integer types:
- Represent whole numbers
- May be signed or unsigned
- Have range of values, depending on thesize of memory used
- The default value of integer types is:
- 0 – for integer types, except
- 0L – for the long type
Integer Types
- Integer types are:
- 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): unsigned16-bit
- int (-2,147,483,648 to 2,147,483,647): signed 32-bit
- uint (0 to 4,294,967,295): unsigned 32-bit
Integer Types (2)
- More integer types:
- 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
What are Floating-Point Types?
- Floating-point types:
- Represent real numbers
- May be signed or unsigned
- Have range of values and different precision depending on thesizeof memory used
- Can behave abnormally in the calculations
Floating-Point Types
- Floating-point types are:
- float (±1.5 × 10−45to±3.4 × 1038):32-bits, precision of7digits
- double (±5.0 × 10−324to±1.7 × 10308):64-bits, precision of15-16digits
- The default value of floating-point types:
- Is 0.0F for the float type
- Is 0.0D for the double type
PI Precision – Example
- See below thedifference 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
Abnormalities in the Floating-Point Calculations
- Sometimes abnormalitiescan be observedwhen using floating-point numbers
- Comparing floating-point numbers can not beperformed directlywiththe == operator
- Example:
Decimal Floating-Point Types
- There is a specialdecimal floating-point realnumbertype in C#:
- decimal (±1,0 × 10-28to±7,9 × 1028): 128-bits, precision of28-29digits
- Used for financialcalculations
- No round-offerrors
- Almost no loss of precision
- The default value of decimal type is:
- 0.0 M ( M is the suffix for decimal numbers)
The Boolean Data Type
- The Boolean data type :
- Is declaredbythe bool keyword
- Has two possible values: true and false
- Is useful in logical expressions
- The default value is false
The Character Data Type
- 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 )
The String Data Type
- 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 beconcatenated
The Object Type
- The object type:
- Is declared by the object keyword
- Is thebase typeof all other types
- Canhold values of any type
What Is a Variable?
- A variable is a:
- Placeholder of information that can usuallybe changedat run-time
- Variables allow you to:
- Store information
- Retrieve the stored information
- Manipulate the stored information
Variable Characteristics
- A variable has:
- Name
- Type (of stored data)
- Value
- Example:
- Name: counter
- Type: int
- Value: 5
Declaring Variables
- 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:
Identifiers
- Identifiers may consist of:
- Letters (Unicode)
- Digits [0-9]
- Underscore “_”
- Identifiers
- Can begin only with a letter or an underscore
- Cannot be a C# keyword
Identifiers (2)
- Identifiers
- Shouldhave adescriptive name
- It is recommended to use only Latin letters
- Should be neither too long nor too short
- Note:
- In C# smallletters are considered different thanthe capitalletters (case sensitivity)
Assigning Values
- Assigning ofvaluestovariables
- Is achieved by the = operator
- The = operator has
- Variable identifier on the left
- Value of the corresponding data type on the right
- Could be used in a cascade calling, where assigning is done from right to left
Initializing Variables
- Initializing
- Is assigning of 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
What are Literals?
- Literals are:
- Representationsofvalues in the source code
- There are six types of literals
- Boolean
- Integer
- Real
- Character
- String
- The null literal
Boolean and Integer Literals
- The boolean literals are:
- 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
Integer Literals
- Examples of integer literals
- The ’ 0x ’ and ’ 0X ’ prefixesmean a hexadecimal value, e.g. 0xA8F1
- The ’ u ’ and ’ U ’ suffixesmean a ulong or uint type, e.g. 12345678U
- The ’ l ’ and ’ L ’ suffixesmean a long or ulong type, e.g. 9876543L
Real Literals
- The real literals:
- Are used for values of type float , double and decimal
- May consist of digits, a sign and“ . ”
- May be in exponentialnotation: 6.02e+23
- The“ f ”and“ F ”suffixes mean float
- The “ d ” and “ D ” suffixes mean double
- The “ m ” and “ M ” suffixes mean decimal
- Thedefault interpretation is double
Character Literals
- The character literals:
- Are used for values of the char type
- Consist of two single quotes surrounding thecharacter value: ’ <value> ’
- The value may be:
- Symbol
- The code of the symbol
- Escapingsequence
Escaping Sequences
- Escapingsequences are:
- Means of presenting a symbol that is usually interpreted otherwise (like ’ )
- Means of presenting system symbols (like the new line symbol)
- Commonescapingsequences are:
- \’ for singlequote \" for double quote
- \\ forbackslash \n for newline
- \uXXXX for denoting any other Unicode symbol
String Literals
- String literals:
- Are used for values of the string type
- Consist of two double quotes surrounding the value: " <value> "
- May have a @ prefix whichignores theusedescaping sequences: @ “<value>”
- The value is a sequence of character literals
Nullable Types
- Nullable types are instances of the System.Nullable struct
- Wrapper over 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
Dynamic Types
- Dynamic types in C# (keyword dynamic )
- Can hold anything (string, number, object, function / method reference)
- Operations evaluated at runtime
- Behave like types in JavaScript / PHP