0% found this document useful (0 votes)
34 views40 pages

Click To Edit Master Subtitle Style

The document discusses .NET properties, indexers, delegates, and events. It explains that properties allow reading and writing class fields through get/set accessors. Indexers provide array-like access to class members. Delegates reference methods and allow calling them through the delegate name. Events allow classes to notify subscriber objects when something occurs, by defining an event, subscribers registering handlers, and the publisher raising the event.

Uploaded by

Maheen Iqbal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views40 pages

Click To Edit Master Subtitle Style

The document discusses .NET properties, indexers, delegates, and events. It explains that properties allow reading and writing class fields through get/set accessors. Indexers provide array-like access to class members. Delegates reference methods and allow calling them through the delegate name. Events allow classes to notify subscriber objects when something occurs, by defining an event, subscribers registering handlers, and the publisher raising the event.

Uploaded by

Maheen Iqbal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Even Handling in .

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

Properties with only a get accessor are called Read-Only properties.

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

Properties and Fields

Properties are logical fields


With set and get we can perform validations before assigning the value similarly we can return a calculated value

Properties are an extension of fields


we can declare properties with any access modifier. They can be even static

Unlike fields, properties do not correspond directly to a storage location


Properties refer to the get/set methods while the fields refer directly to the storage location

4/26/12

Properties and Methods

Properties do not use parenthesis. Properties cannot be void.

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

Defining an Indexer [1]

Syntax:

Access Modifier return type this [data type variable]

4/26/12

Defining an Indexer [2]

Rules:

At least one Indexer parameter must be specified. The parameters should be assigned values.

4/26/12

Indexers vs. Arrays [1]

Indexers do not point to memory locations. Indexers can have non-integer subscripts(indexes). Indexers can be overloaded.

4/26/12

Indexers vs. Arrays [2]

4/26/12

Indexers vs. Arrays [3]

4/26/12

Continued ..

Indexers vs. Arrays [4]


Continued ..

4/26/12

Indexers vs. Arrays [5]

4/26/12

Continued ..

Indexers vs. Arrays [6]


Continued ..

4/26/12

Continued ..

Indexers vs. Arrays [7]


Continued ..

4/26/12

Multiple Parameters in Indexers

More than one indexer parameter can be specified.

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

Delegates can best be seen as a new type of object in C#.

this 4/26/12 name.

Defining a Delegate

Syntax:
Access Modifier delegate void DelegateName()

4/26/12

Instantiating Delegates [1]


Instantiating a delegate means pointing it to some method. To instantiate a delegate, we must call the constructor of the delegate and pass the method (along with its object name), to be associated with the delegate, as its parameter.

4/26/12

Instantiating Delegates [2]

4/26/12

Continued ..

Instantiating Delegates [3]


Continued ..

4/26/12

Using Delegates [1]


Using a delegate means instantiating a method using delegates.

4/26/12

Continued ..

Using Delegates [1]


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

Subscribe objects to that event

Notify subscribers of the event

4/26/12

Defining Events
The Publisher,

defines a delegate defines an event based on the delegate

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.

You might also like