Click To Edit Master Subtitle Style
Click To Edit Master Subtitle Style
Net
Click to edit Master subtitle style
4/26/12
Objectives
Explain Properties Implement Indexers Implement Delegates Define and raise Events
4/26/12
Properties [1]
C# provides the facility to protect a field in a class by reading and writing to it through a feature called Properties. With Properties, we just have to define the Set/Get methods and continue to use the data members as fields. The runtime takes care of identifying and calling the appropriate Set/Get function.
4/26/12
Properties [2]
4/26/12
Continued ..
Properties [3]
Continued ..
4/26/12
Properties [4]
4/26/12
Types of Properties
Read/Write
ReadOnly
Write-Only
4/26/12
Read/Write Property
Properties of this type provide both read and write access to the data members.
4/26/12
Read-Only Property
4/26/12
Write-Only Property
Properties with only a set accessor are called Write-Only properties. We can only write values to it but can never retrieve the value.
4/26/12
4/26/12
4/26/12
Indexers
An indexer allows an object to be indexed in the
same way as an array. As properties provide us with the field-like access to the data of an object, indexers enable array-like access to our class members
4/26/12
Indexers [1]
4/26/12
Continued ..
Indexers [2]
Continued ..
Output:
4/26/12
Steps
Mention the access modifier, which decides the visibility
4/26/12
of the indexer State the return type of the indexer (what the get accessor returns) Specify the this keyword (this is the name of the indexer, it should always be this) Insert the open square brackets Specify the data type of the index (indexers unlike arrays, can also be indexed on string, or any other data type). State the variable name for the index, followed by the close square brackets Insert the open curly braces. The get and set accessors are specified here, just as in the case of properties. Finally, the close braces must be inserted
Syntax:
4/26/12
Rules:
At least one Indexer parameter must be specified. The parameters should be assigned values.
4/26/12
Indexers do not point to memory locations. Indexers can have non-integer subscripts(indexes). Indexers can be overloaded.
4/26/12
4/26/12
4/26/12
Continued ..
4/26/12
4/26/12
Continued ..
4/26/12
Continued ..
4/26/12
4/26/12
Delegates
A delegate contains a reference to a method. A delegate connects a name with the specification of a method. An implementation of some method can be then attached to this name. A component can call the method by using
Defining a Delegate
Syntax:
Access Modifier delegate void DelegateName()
4/26/12
4/26/12
4/26/12
Continued ..
4/26/12
4/26/12
Continued ..
Output:
4/26/12
Events [1]
Events in C# allow an object to notify other objects about the event, or that a change has The object that notifies others about the occurred. event is known as the Publisher. An object that registers to an event is known as the Subscriber.
4/26/12
Events [2]
Steps:
Define an event
4/26/12
Defining Events
The Publisher,
4/26/12
Subscribing to an Event
Subscribing an object to an event depends on whether the event exists. If the event exists, then the subscribing object simply adds a delegate that calls a method when the event is raised.
4/26/12
Notifying Objects
To notify all the objects that have subscribed to an event, we just need to raise the event:
4/26/12
Events [Cont]
Output.