190 likes | 393 Views
Reference Types. (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay dundee. Last updated 22nd October 2004. Agenda. Reference Types Object Types The Object & String Classes Interface types Pointer Types Assignment Compatibility. Reference Types ….
E N D
ReferenceTypes (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay dundee Last updated 22nd October 2004
Agenda • Reference Types • Object Types • The Object & String Classes • Interface types • Pointer Types • Assignment Compatibility
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.
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"; reference : strongly typed to a String str location : can only contain String values variable name "cat" … … value: an object of type String
Reference Type Categories • Object types • represent classes • Interface types • represent interfaces • Pointer types • represent typed addresses
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.
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.
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.
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).
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.
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;
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
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).
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.
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).
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
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.
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