Reference Types - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Reference Types

Description:

combine a reference identity to a location (address) with the value stored in ... return new String objects rather than change the current object (immutability) ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 19
Provided by: mct3
Category:

less

Transcript and Presenter's Notes

Title: Reference Types


1
ReferenceTypes
(c) Allan C. Milne School of Computing Creative
Technologies University of Abertay dundee
Last updated 22nd October 2004
2
Agenda
  • Reference Types
  • Object Types
  • The Object String Classes
  • Interface types
  • Pointer Types
  • Assignment Compatibility

3
Reference Types
  • are fully inheritable.
  • combine a reference identity to a location
    (address) with the value stored in that location.
  • values are accessed through strongly-typed
    references.
  • are updated if the location is moved by the
    garbage collector.

4
A Reference Type Example
  • Here is an example of declaring a variable of an
    object reference type and initialising it to a
    reference to an object value.

String str "cat"
str
reference strongly typed to a String
location can only contain String values
variable name
"cat"
value an object of type String
5
Reference Type Categories
  • Object types
  • represent classes
  • Interface types
  • represent interfaces
  • Pointer types
  • represent typed addresses

6
Object Types
  • The CLS specifies two built-in object types with
    support in the CLR
  • Object (System.Object)
  • String (System.String)
  • Other types provided in the .Net Framework are
    considered as user-defined object types.
  • All user-defined object types inherit (directly
    or indirectly) from the CLR type Object.
  • Note that object type and class are synonymous.

7
The Object Class
  • The base type for all types in the CLR.
  • Enforces a single CLR supported type inheritance
    hierarchy for all types.
  • Includes value types since boxing allows these to
    be treated as subtypes of Object.
  • Provides a poymorphic type that can be used for
    return types parameters in generalised methods.

8
Object Methods
  • Object provides anumber of public methods that
    can be used with all types or over-ridden as
    required
  • bool Equals (Object o)
  • int GetHashCode ()
  • Type GetType ()
  • static bool ReferenceEquals (Object o1,
    Object o2)
  • String ToString ()
  • There are also a number of protected methods that
    can be useful.

9
The String Class
  • is a built-in object type in the CLR.
  • is a sealed class (for efficiency).
  • defines values (objects) that are immutable.
  • uses 16-bit Unicode characters in its string
    representation.
  • has many methods providing great functionality.
  • has "mutator" methods that return new String
    objects rather than change the current object
    (immutability).

10
Interface Types
  • support programming with interfaces.
  • provide a mechanism for relating classes which do
    not share implementation inheritance but do share
    some common method bahaviour.
  • are partial specifications of types with member
    types but no implementations.
  • may be supported by many classes and a class may
    support many interface types.

11
An Interface Example
interface IPosition int X get int
Y get void Place (int x,int y)
class Eg IPosition private int xpos,
ypos public int X get return xpos
public int Y get return ypos
public void Place (int x,int y) xpos
x ypos y
Eg myEg new Eg() myEg.Place (12, 34) int
a myEg.X int b myEg.Y Iposition ip
myEg ip.Place (67, 89) int i ip.X int j
myEg.Y
12
Interface Types Members
  • There are no built-in interface types.
  • The .Net Framework provides a number of
    interfaces.
  • An interface type can contain fields, methods,
    properties and events.
  • The methods, properties events must, by
    definition, be
  • public available to all users of the interface
  • abstract body must be defined in using class
  • virtual execution-time binding used

13
Pointer Types
  • are included here only for completeness, not for
    use.
  • represent the locations of code or values (I.e.
    are addresses).
  • will usually be abstracted by a programming
    language so as to be invisible to programs.
  • are useful in understanding the .Net platform but
    dangerous to use in programs.
  • are open to misuse by going outside the semantics
    of the .Net managed code environment (especially
    the gc).

14
Pointer Types Supported
  • Managed pointers
  • known to the garbage collector.
  • Unmanaged function pointers
  • refer to code
  • Unmanaged pointers
  • refer to values.
  • are not CLS compliant.

15
Assignment Compatibility
  • A variable of object or interface type T can
    refer to any object
  • of type T, or
  • of a subtype of T, or
  • that supports the interface T.
  • The above rules apply when checking if the left
    and right parts of an assignment are compatible.
  • Assignment may be explicit ('' operator) or
    implicit (e.g. parameter passing).

16
Compatibility Example
int i 42 Object o
null String s "Allan" IComparable ic
null
// compatible o i // uses boxing o
s o ic ic i // uses boxing ic s
// incompatible i o i s i ic s i s
o s ic ic o
// run-time incompatibility o i s (String)
o
  • see CompatibilityEg1.cs

17
Some Tidying Up
  • An object type (class) can inherit from another
    (single) object type and/or multiple interfaces
  • The CLR supports a number of member accessibility
    options although in C public, private and
    protected are used.
  • Methods may be overloaded over-ridden.
  • Virtual methods are supported by run-time
    dispatching.

18
An Example of Inheritance
class PlacedBall SnookerBall,
IPosition private int xpos, ypos
public int X get return xpos
public int Y get return ypos public
PlacedBall (BallColour c) base (c)
public void Place (int x, int y) xpos
x ypos y
PlacedBall ball ball new PlacedBall
(BallColour.Red) ball.Place (12,34) int v
ball.Value int x ball.X IPosition ip
ball ip.Place (67, 89) int ipx ip.X int bx
ball.X
  • see PlacedBall.cs
Write a Comment
User Comments (0)
About PowerShow.com