1 / 36

FDSc

FDSc. Module 201 Object Oriented Programming Lecture 2 – Data Types. Previously. Variables. Objectives. Scope Variable lifetime Data types. Defining Scope. A variable comes into existence when it is created When we use a variable it is said to be in scope at that location.

livi
Download Presentation

FDSc

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FDSc Module 201 Object Oriented Programming Lecture 2 – Data Types

  2. Previously... • Variables

  3. Objectives • Scope • Variable lifetime • Data types

  4. Defining Scope • 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

  5. Defining Scope • The opening and closing braces of a method define the scope. • Variables within this method are called local variables class Example { void method1() { intsomeVariable; ... } void method2() { someVariable = 42; } } What error would occur here? Variable not in scope

  6. Defining Class Scope class Example { void method1() { someVariable = 56; //Ok ... } void method2() { someVariable = 42; //OK } intsomeVariable = 0; } Defined within the class and therefore has class scope

  7. Variable Lifetime Static void VariableLifetime() { InttotalAmount = 100 totalAmount = totalAmount + 100; } Static inttotalAmount; //Declared at class level Static void VariableScope() { totalAmount= 100; ShowTotalAmount(); } ShowTotalAmount() { Console.WriteLine(“Total amount is {0}”, totalAmount); }

  8. Data Types • 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

  9. Integer Data Types • sbyte – based on System.Sbyte • Signed 8-bit integer between -128 and 127 • byte – based on System.Byte • 8-bit integer between 0 and 255 • short – based on System.Int16 • 16-bit integer between -32768 and 32767 • ushort - based on System.Int16 • 16-bit unsigned integer between 0 and 65535 • int – based on System.Int32 • 32-bit integer between -2147483648 and 2147483647 • uint - based on System.Int32 • 32-bit integer between 0 and 4294967295 • long - based on System.Int64 • 64-bit integer between -9223372036854775808 and 9223372036854775807 • ulong - based on System.Int64 • Unsigned 32-bit integer between 0 and 18446744073709551615

  10. How to Choose an Integer Data Type • 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

  11. Data Types Fields and Methods • 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

  12. Floating Point Data Types • Float – based on System.Single • 32-bit single-precision floating point number between -3.402823 x 1038and -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 10308and -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

  13. Decimal • 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

  14. Decimal Data Types Fields and Methods • 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

  15. Char Data Type • 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

  16. Char Data Type Methods • 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

  17. String Data Types • 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 "" ";

  18. Bool Data Type • 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); }

  19. Object Data Type • 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

  20. Converting to Another Data Type • 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

  21. Converting To Another Data Type • 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());

  22. Value Types and Reference Types • 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

  23. Boxing • 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

  24. Unboxing • 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

  25. Constants • Declared like variables • Value cannot be changed in code

  26. Enumerations • Collection of related constants • Has a name and a numeric data type • Has a number of fields, each with a name and a value

  27. Structs • Structs (structures) are user defined data types • Similar to enumerations in that they are a collection of values • Can contain any data type

  28. Operators • 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

  29. Arithmetic 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

  30. String Operators • + 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”

  31. Assignment Operators • 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

  32. Comparison Operators • 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

  33. Logical Operators • 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

  34. Type Operators • 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”); }

  35. Summary • Scope • Variable lifetime • Data types

  36. Next time

More Related