FDSc

Module 201
Object Oriented Programming
Lecture 2 – Data Types

Variables



Scope
Variable lifetime
Data types
A variable comes into existence when it is created
When we use a variable it is said to be in scope at
that location.
 We can then use that variable in the same method
using a variety of statements
 When the method is finished, the variable
disappears, and so does the scope of that variable




The opening and closing braces of a method define the scope.
Variables within this method are called local variables
class Example
{
void method1()
{
int someVariable;
...
}
void method2()
{
someVariable = 42;
}
}
What error would occur here?
Variable not in scope
class Example
{
void method1()
{
someVariable = 56; //Ok
...
}
void method2()
{
someVariable = 42; //OK
}
}
int someVariable = 0;
Defined within the class and therefore has class scope
Static void VariableLifetime()
{
Int totalAmount = 100
totalAmount = totalAmount + 100;
}
Static int totalAmount; //Declared at class level
Static void VariableScope()
{
totalAmount = 100;
ShowTotalAmount();
}
ShowTotalAmount()
{
Console.WriteLine(“Total amount is {0}”, totalAmount);
}
All information has a type
Type defines how information will be stored, used,
manipulated and displayed
 .NET Framework contains structures and classes
that represent various types of data
 All data types in VB and C# are based on a .NET
Framework class or structure



sbyte – based on System.Sbyte

byte – based on System.Byte

short – based on System.Int16

ushort - based on System.Int16

int – based on System.Int32

uint - based on System.Int32

long - based on System.Int64

ulong - based on System.Int64
 Signed 8-bit integer between -128 and 127
 8-bit integer between 0 and 255
 16-bit integer between -32768 and 32767
 16-bit unsigned integer between 0 and 65535
 32-bit integer between -2147483648 and 2147483647
 32-bit integer between 0 and 4294967295
 64-bit integer between -9223372036854775808 and
9223372036854775807
 Unsigned 32-bit integer between 0 and 18446744073709551615


Choose data type that is appropriate
Balance memory requirement and performance
 int requires twice as much storage space (4 bytes) than short (2
bytes)
 int and long are more efficient than byte or short because .NET
Frameworks represents numbers as 32-bit or 64-bit values

Use int unless you have concerns about memory
Data types are based on structures
A structure is a type of class
Structure uses methods and fields
Data Types have a MinValue and a MaxValue which
represent low and end of range of values
 ToString returns string representation of value




 Specify format to control how string is displayed

Float – based on System.Single
 32-bit single-precision floating point number between -3.402823
x 1038 and -1.401298 / 1045 for negative values and between
1.401298 / 1045 and 3.402823 x 1038 for positive values

Double – based on System.Double
 64-bit double-precision floating point number between 1.79769313486232 x 10308 and -4.94065645841246544 / 10324 for
negative numbers and between 1.79769313486232 / 10324 and
1.79769313486232 x 10308
Use double unless you have valid concerns about
memory usage

 More accurate

Based on System.Decimal
 128-bit number between -79228162514264337593543950335 and
79228162514264337593543950335 with no decimal places and 7.9228162514264337593543950335 and
7.9228162514264337593543950335 with up to 28 decimal places
 Holds numbers of lesser magnitude than floating points but
with greater precision
 Use when you need utmost precision
Truncate – returns integer part and discards
fractional part
 Round – rounds to nearest integer or to a specified
number of decimal places
 Floor – rounds to integer smaller or equal to the
value
 Ceiling – rounds to integer greater than or equal to
the value


Based on System.Char
 16-bit numeric value between 0 to 65535
Holds code points, or character codes, representing
a single Unicode character

 The first 128 code points, number 0 to 127 arethe ASCII character
set
 ConvertFromUtf32 – returns Unicode characters associated with code
point
 ConvertToUtf32 – returns code point associated with Unicode
character
 IsControl – indicates if a tab, carriage return or line feed
 IsDigit – indicates if a decimal number
 IsLetter – indicates if a letter
 IsLetterOrDigit – indicates if a letter or a decimal number
 IsLower – indicates if a lower case letter
 IsUpper – indicates if upper case letter
 IsNumber – indicates if a number
 IsPunctuation – indicates if punctuation character
 IsSeparator – indicates if separator character
 IsSymbol – indicates if symbol
 IsWhiteSpace – indicates if whitespace

Based on System.String
 Represents a series of 0 to 2 billion characters
Use escape sequences (\) or preface with @ to
indicate quotation marks and backslashes in strings

 E.G.
String greeting1 = "Hello \ " Robert\ " ";
String greeting2 = @ “Hello " " Robert " " ";




Based on System.Boolean
0 (true) or 1 (false)
Used to test a condition
E.G.
if(firstVariable > secondVariable)
{
Console.WriteLine(“{0} is greater than {1}“, firstVariable, secondVariable);
}
Based on System.Object
Everything ultimately is derived from Object
Highest level object there is in the .NET Framework
Can contain any data type, including another object
Use GetType to determine what type of data is
stored
 Contains a pointer to the value in memory, NOT
actual data
 Always needs an extra step therefore is not efficient






Widening conversion
 New data type can store all of the values of the original data
type
 E.g. 32-bit integer to decimal (decimal can store everything 32-bit int
can)
 Compiler will make the conversion for you

Narrowing conversion
 New data type cannot store all of the values of the original data
type
 E.g. decimal to int or int to byte could result in data loss
 Need to make the conversion in code
 Make your conversion explicitly in code
 Code is more readable and understandable

Use a cast operator
 shortValue = (short)(shortValue + byteValue);
Convert class includes a conversion method for each
data type

 Convert.ToSingle(longValue);

Parse method converts a string to a data type
 Single.Parse(longValue.ToString());

Value type: variables directly store their value
 Stored in the stack, pool of memory allocated by runtime for value
types
 Declared in code and runtime allocates proper amount of memory for
them
 Efficient because space has already been allocated on the stack
(memory)
Reference type: variables store a reference to their
values





Stored in the heap, a pool of memory whose size is dynamic
Runtime will allocate memory as needed
Value is stored in the stack, but variable stores reference to value
Reference is used to find the value each time the variable is accessed
in code
 Less efficient than value types
A value type is converted to a reference type
.NET Framework copies the value to the heap and
returns a reference to the value


 E.g. store 7 in an object variable
A reference type is converted to a value type
.NET Framework uses reference to copy the value
back into a value type


 E.g. 7 stored in an object: .NET has a reference to the 7, goes and
retrieves that and then brings that back
Both of these (boxing and unboxing) are less
efficient than using value types



Declared like variables
Value cannot be changed in code
Collection of related constants
Has a name and a numeric data type
Has a number of fields, each with a name and a
value



Structs (structures) are user defined data types
Similar to enumerations in that they are a collection
of values
 Can contain any data type


Perform an action on one or more values and return
the result of the operation







Arithmetic operators
String operators
Assignment operators
Comparison operators
Logical operators
Type operators

Perform basic arithmetic on one or more variables
 + adds two numbers or converts a negative number into a
positive number
 - subtracts two numbers or converts a positive number into a
negative number
 * multiplies two numbers
 / divides two numbers
 % divides two numbers and returns only the remainder of the
result
 ++ increments a number by 1
 -- decrements a number by 1
+ operator concatenates, or adds, two strings
together to produce a new string

String sentence1 = “Hello “;
String sentence2 = “Joe”;
String sentence3;
Sentence3 = sentence1 + sentence2

This will return “Hello Joe”

Perform similar operations as arithmetic operators
 += adds two numbers or converts a negative number into a
positive number
 -= subtracts two numbers or converts a positive number into a
negative number
 *= multiplies two numbers
 /= divides two numbers
 %= divides two numbers and returns only the remainder of the
result

Used to compare two values





== returns true if two values are equal
!= returns true if two values are not equal
> returns true if the first value is greater than the second value
< returns true is the first value is less than the second value
>= returns true is the first value is greater than or equal to the
second value
 <= returns true if the first value is less than or equal to the
second value

Used to compare two expressions




A & B returns true is both A and B are true
A | B returns true if either A or B is true
!A returns true if A is not true
A ^ B returns true if either A or B is true but both of them is not
true
 A && B returns true if both A and B are true. Does not evaluate
B if A is not true
 A || B returns true if either A or B is true. Does not evaluate B if A
is true

Test whether an object is of a particular data type
Object object1;
Object1 = 7;
If (object1 is int)
{
Console.WriteLine(“object1 = 7 and is type Integer”);
}



Scope
Variable lifetime
Data types