Professional Documents
Culture Documents
Advanced C# Concepts II
Review
C# Simplified / Session 6 / 2 of 44
Review Contd
C# Simplified / Session 6 / 3 of 44
Objectives
Explain Properties
Public
Private
Read-only
Write-only
Discuss Indexers
Implement Delegates
C# Simplified / Session 6 / 4 of 44
Properties in C#
C# Simplified / Session 6 / 5 of 44
Properties in C# - Contd
C# Simplified / Session 6 / 6 of 44
C# Simplified / Session 6 / 7 of 44
C# Simplified / Session 6 / 8 of 44
Properties - Example
using System;
public class Student
{
public string sName;
//Field
private string internal_sId;//Field
public string sId
//Property
{
get
{
return internal_sId;
}
set
{
internal_sId = value;
}
}
}
C# Simplified / Session 6 / 9 of 44
C# Simplified / Session 6 / 10 of 4
Properties - Explanation
C# Simplified / Session 6 / 11 of 4
Types of Properties
C# Simplified / Session 6 / 12 of 4
C# Simplified / Session 6 / 13 of 4
C# Simplified / Session 6 / 14 of 4
Properties Vs Fields
C# Simplified / Session 6 / 15 of 4
Property Accessors
C# Simplified / Session 6 / 16 of 4
Indexers
C# Simplified / Session 6 / 17 of 4
Indexers - Example
class IndexerExample
{
public string[] stringList =new string[10];
public string this[int index]
{
get
{
return stringList[index];
}
set
{
stringList[index] = value.ToString();
}
}
}
C# Simplified / Session 6 / 18 of 4
Indexers - Output
class Test
{
static void Main()
{
IndexerExample indexTest = new IndexerExample();
indexTest.stringList[1]="Sam";
indexTest[2]="Tom";
System.Console.WriteLine("indexTest[1] is
{0}\nindexTest[2] is {1}", indexTest[1], indexTest[2]);
}
}
C# Simplified / Session 6 / 19 of 4
C# Simplified / Session 6 / 20 of 4
C# Simplified / Session 6 / 21 of 4
C# Simplified / Session 6 / 22 of 4
Indexer Example 1
using System.Collections;
class StrIndex
{
public Hashtable studentList = new Hashtable();
public int this[string name]
{
get
{
return (int) studentList[name];
}
set
{
studentList[name] = value;
}
}
}
C# Simplified / Session 6 / 23 of 4
Indexer Example 1
Contd
class Test
{
static void Main()
{
StrIndex objIndex = new StrIndex();
objIndex ["Sam"] = 232676;
objIndex ["Tom"] = 455464;
System.Console.WriteLine ("Phone number
of Sam is {0} and Phone number of Tom
is {1}", objIndex ["Sam"],
objIndex["Tom"]);
}
}
C# Simplified / Session 6 / 24 of 4
Indexer Example - 2
using System.Collections;
class IndexerExample
{
public string[] stringList = new string[10];
public string this[int index]
{
get
{
return stringList[index];
}
set
{
stringList[index] = value.ToString();
}
}
public Hashtable studentList = new Hashtable();
C# Simplified / Session 6 / 25 of 4
Indexer Example 2
Contd
public int this[string number]
{
get
{
return (int) studentList [number];
}
set
{
studentList [number] = value;
}
}
}
C# Simplified / Session 6 / 26 of 4
Indexer Example 2
Contd
class Test
{
static void Main()
{
IndexerExample indexTest = new IndexerExample();
indexTest.stringList[1] = "Sam";
indexTest[2] = "Tom";
indexTest ["Sam"] = 232;
indexTest ["Tom"] = 455;
}
}
C# Simplified / Session 6 / 27 of 4
...
class TestMultip
{
public int this[int firstP, int secondP]
{
//Get and Set Accessors appear here
}
}
. . .
C# Simplified / Session 6 / 28 of 4
{
void static Main()
{
TestMultip myTest = new TestMultip();
int I = myTest[1,1];
}
}
C# Simplified / Session 6 / 29 of 4
Delegates
Defining a delegate
Instantiating a delegate
Using a delegate
C# Simplified / Session 6 / 30 of 4
Defining Delegates
Defining a delegate involves specifying the
return types and parameters that each method
must provide
public delegate void
DelegateName();
...
public delegate int CallFunctions (int a, int b);
...
C# Simplified / Session 6 / 31 of 4
Instantiating Delegates
Instantiating a
class TestDelegates
{
delegate
public delegate int CallFunctions(int a, int b);
means making
class MathsOperations
it point (or
{
public int MultiplyFn(int a, int b)
refer) to some
{
method.
return a*b;
}
public int DivisionFn(int a, int b)
{
return a/b;
}
}
C# Simplified / Session 6 / 32 of 4
Instantiating Delegates
Contd
class Test
{
static void Main()
{
CallFunctions DelegateObj;
MathsOperations math = new
MathsOperations();
DelegateObj = new
CallFunctions(math.MultiplyFun);
}
}
}
C# Simplified / Session 6 / 33 of 4
Using Delegates
class TestDelegates
{
public delegate int CallFunctions(int num1, int
num2);
class MathsOperations
{
public int MultiplyFn(int num1, int num2)
{
return num1*num2;
}
public int DivisionFn(int num1, int num2)
{
return num1/num2;
}
}
}
C# Simplified / Session 6 / 35 of 4
C# Simplified / Session 6 / 36 of 4
Events
Steps
Defining an Event
Subscribe objects to that event
Notify subscribers of the event
C# Simplified / Session 6 / 37 of 4
Defining Events
defines a delegate
defines an event based on the delegate
C# Simplified / Session 6 / 38 of 4
Subscribing
Subscribing an object to an event
depends upon the existence of the
event
eventMe += new delegateMe(objA.Method);
eventMe += new delegateMe(objB.Method);
C# Simplified / Session 6 / 39 of 4
Notifying
To notify all the objects that have
subscribed to an event we just need
to raise the event
if(condition)
{
eventMe();
}
C# Simplified / Session 6 / 40 of 4
C# Simplified / Session 6 / 41 of 4
C# Simplified / Session 6 / 42 of 4
C# Simplified / Session 6 / 43 of 4
Summary
Defining a Delegate
Instantiating a Delegate
Using a Delegate
Defining an Event
Subscribing objects to that event
Notifying subscribers of the event (when it occurs)
C# Simplified / Session 6 / 44 of 4