180 likes | 322 Views
Structure and Class Members. (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay dundee. Last updated 20 th October 2005. Agenda. Fields Properties Methods Static & instance members Events Delegates. Introduction.
E N D
Structureand ClassMembers (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay dundee Last updated 20th October 2005
Agenda • Fields • Properties • Methods • Static & instance members • Events • Delegates
Introduction • This presentation introduces the different types of components that can be used as members of a class (or structure). • We will focus here on members of classes (the reference type) rather than of structures (the value type) although much of the content is applicable to both. • Members also have individual accessibility (public, private or protected) that defines their accessibility to other programming entities; this will only be covered here in passing.
Members • A class or structure is constructed from its individual members. • Members are named and typed entities that define both • the public class behaviour (to users of the class), and • the private state and mechanisms that form the internal workings of the class. • Members can be fields, properties, methods or events.
Fields (Attributes) … • are used to store values that normally represent the internal state of an object. • have a name and a type. • should be defined as private; • hidden from outside users of the class, and • only accessible by other class members. • often have properties associated with them to provide a public exposure.
Properties … • are supported by the CLR through metadata annotations. • expose the state of objects of the class. • are usually defined as public; • accessible from outside the class, and • together with methods, provide the external behaviour of the class. • can be read-only (get), write-only (set) or read-write (get and set).
A Field & Property Example class Staff { private String name; private int room; private String email; public String Name { get { return name; } } public int Room { get { return room; } set { room = value; } } public String Email { set { email = value; } } public Staff (String name) { this.name = name; } } // using the class Staff me; me = new Staff ("Allan"); String myName = me.Name; me.Room = 4519; int myRoom = me.Room; me.Email = "fred@here"; see Programs\Staff1.cs
Properties also … • have a name, type with get and/or set accessor methods. • are accessed as if they were public fields for reading and writing. • represent logical fields of the class; • there is no requirement for any relationship between an actual field and a property, • get property values can be computed, and • set property operations may result in some general change of state.
Public Properties Vs Puclic fields • Provide protection through control over the read-write operations available. • Assures data integrity through allowing appropriate validation to be performed on write operations. • Contributes to removing versioning issues through distinguishing between logical fields and physical implementation.
Methods … • define the behaviour of the class. • form a contract between the class and users of the class. • have (i.e. the contract is defined by) • a name, • a parameter list (possibly empty), and • a return type. • are called by users of the class by supplying the method name and an actual parameter list.
Method Accessibility • Most methods will be defined as public; • accessible outside the class • provide the external behaviour of the class. • Methods can also be defined as private; • hidden from outside users • only accessible by other class members • provide any internal structuring of functionality required.
Static Members … • have only one copy at run-time. • are invoked on the class type itself. • can be called at any time (even if no class objects exist). class Eg1 { public static void Hi () { Console.WriteLine ("A static Hi"); } } … … … Eg1.Hi();
Instance Members … • have a copy in each object of the class. • are invoked on an explicit object. • can only be called when an object of the class type has been created. class Eg2 { public void Hi () { Console.WriteLine ("An instance Hi"); } } … … … Eg2 obj = new Eg2(); obj.Hi();
Events … • expose the processing of asynchronous changes in an object. • are supported directly in the CLR through generated methods and associated metadata. • have a name and a type. • types specify the method signature that users must supply for the event-handler method; this is a delegate type.
Event-Handler Methods … • are user-defined methods that implement the handling of an event. • are also known as callback methods or listeners. • must match the method signature specified in the event type (the delegate type). • are the methods that will be called when the associated event is raised. • must be registered with an event through attaching an appropriate delegate.
Delegates … • are an object-oriented implementation of a type-safe function pointer. • are supported by the CLR. • are used to specify callback methods for events. • are also used in many other non-event contexts.
Outline of Event Usage • Define an appropriate delegate type for the event-handling (callback) method. • Define a structure or class that exposes a member of an event type. • Create a value of the class type. • Attach event-handling (callback or listener) methods for the event. • Raise the event.
Event Example public delegate void Handler (int n); class EventEg1 { public event Handler MyEvent; public void DoSomething (int n) { MyEvent (n); } } class EventEg1App { public static void Main () { EventEg1 myEg = new EventEg1(); myEg.MyEvent += new Handler(Cat); myEg.DoSomething (3); } private static void Cat (int n) { Console.WriteLine ("{0} cats", n); } } seePrograms\EventEg1.cs