C - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

C

Description:

boxing and unboxing. when necessary, C# will auto-convert between value and object ... s1 = 'an apple a day'; s2 = 'keeps the doctor away'; DataTypes..ppt. 12 ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 23
Provided by: patpa
Category:
Tags:

less

Transcript and Presenter's Notes

Title: C


1
C Data Types
2
some common C types
3
type conversion
  • there are various ways to accomplish conversions

int i 5 double d 3.2 string s
"496" d i i (int) d i
System.Convert.ToInt32(s)
implicit conversion
cast required
conversion required
4
value types, vs. reference types
  • value types are C fields that correspond to .NET
    primitives, such as
  • bool, int, float, enum, struct
  • when sent as parameters in a method call, value
    types are allocated on the stack
  • they have a lifetime limited by their scope (as
    in C)
  • they are not allocated on the managed heap
  • reference types
  • fields that act as shortcuts to objects in the
    managed heap

5
value vs. reference types
  • .NET separates data types into two categories
  • value types
  • variable represents a value ("bits")
  • reference types
  • variable represents a reference to a heap-based
    object
  • actual data resides in the object

int i i 10
10
string s s "calico"
6
how do you know which types are which?
  • primitive types (except strings) are value types
  • all other types are references, including arrays
  • why do we care?
  • efficiency
  • comparison

int x, y string s1, s2 Customer c1,
c2 . . . if (x y) ... if (x lt y) ... if
(s1.Equals(s2)) ... if (s1.CompareTo(s2) lt 0)
... if (c1.Equals(c2)) ...
7
boxing and unboxing
  • when necessary, C will auto-convert between
    value and object
  • value gt object is called "boxing"
  • object gt value is called "unboxing"

int i, j object obj string s i
32 obj i // boxed (implicit) i
19 j (int) obj // unboxed via cast
8
The Common Type System (CTS)
  • CTS is based on a hierarchy of classes defined in
    FCL
  • All types inherit from Object (all except
    interface types)

9
user-defined reference types
10
classes
  • classes yield user-defined reference types
  • example
  • Customer class

public class Customer public string Name
// fields public int ID public
Customer(string name, int id) // constructor
this.Name name this.ID id
public override string ToString() // method
return "Customer " this.Name
11
creating objects
  • objects are created using the new operator
  • strings are a special case and don't require the
    use of new

Customer c1, c2 string s1, s2 c1 new
Customer("jim bag", 36259) c2 new
Customer("jane doe", 55298) s1 "an apple a
day" s2 "keeps the doctor away"
12
defining equality
  • all classes inherit from System.Object
  • and it has a method .Equals
  • classes should override Equals
  • example
  • Customers are equal if their IDs are the same

public class Customer . . . public
override bool Equals(object obj) Customer
other if ( obj null
!(obj.GetType().Equals(this.GetType())) )
return false // definitely not equal other
(Customer) obj // type-cast to access id
return this.ID other.ID // equal if same
id...
13
GetHashCode
  • If you override Equals, you must also override
    GetHashCode
  • here's the rule
  • if obj1.Equals(obj2) true, then obj1.GHC()
    obj2.GHC()
  • otherwise, it doesn't matter what GHC returns

public class Customer . . . public
override int GetHashCode() // delegate
hash code computation to underlying integer
class return this.ID.GetHashCode()
14
all objects derive from System.Object
  • user-defined classes can (and sometimes should)
    override some of the System.Object methods,
    including
  • Equals - comparison between objects
  • Finalize - performs cleanup operations before an
    object is automatically reclaimed.
  • GetHashCode - generates a number corresponding to
    the value of the object to support the use of a
    hash table.
  • ToString - manufactures a human-readable text
    string that describes an instance of the class

15
C arrays
16
Arrays
  • Arrays are reference types
  • based on Array class in FxCL
  • must be created using new
  • 0-based indexing
  • assigned default values (0 for numeric, null for
    references, etc.)

int a a new int5 a0 17 a1 32
a4 2 int x a0 a1 a4 int len
a.Length
create
element access
number of elements
17
types of methods
  • classes may contain 2 types of methods
  • subroutines with no return value (void)
  • functions with a return value (int, string, etc.)
  • methods may be
  • static
  • instance (default if static keyword not present)
  • static methods are global and thus require only
    class name
  • instance methods require a reference to an object
    of the class in order to be called

18
Array class in the framework library
  • fully-qualified name is System.Array

namespace System public class Array
public int GetUpperBound(int dimension)
... public static void Sort(Array a)
... . . .
instance method(absence of static)
static method(presence of static)
19
an example of calling into the Array class
  • .Sort is a static method
  • the argument to .Sort is an instance of an Array
  • or an Array object

/ main.cs / using System public class App
public static void Main() int data
11, 7, 38, 55, 3 Array.Sort(data)
for (int i0 iltdata.GetUpperBound(0)
i) Console.WriteLine(i " "
datai)
20
multi-dimensional arrays
  • C supports arrays as a single object, or array
    of arrays (potentially jagged arrays)

Customer, twoD int jaggedD // 2D
array as single object twoD new Customer10,
100 twoD0, 0 new Customer() twoD9, 99
new Customer() // 2D array as array of
arrays jaggedD new int10 jaggedD0 new
int10 jaggedD1 new int20 jaggedD9
new int100 jaggedD00 1 jaggedD999
100
21
foreach
  • specialized foreach loop provided for collections
    like Array
  • reduces risk of indexing error
  • provides read-only access to collection (but
    objects within collection can be modified)

int myArray 1, 2, 3, 4, 5 int sum
0 foreach (int x in myArray) sum x
foreach
type
value
collection
22
the end of this PowerPoint file
Hooray!
Write a Comment
User Comments (0)
About PowerShow.com