C# Starter L06-Delegates, Event Handling and Extension Methods
-
Upload
mohammad-shaker -
Category
Software
-
view
487 -
download
0
description
Transcript of C# Starter L06-Delegates, Event Handling and Extension Methods
![Page 1: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/1.jpg)
Mohammad Shakermohammadshaker.com
C# Programming Course@ZGTRShaker
2011, 2012, 2013, 2014
C# StarterL06 – Delegates, Event Handling and Extension Methods
![Page 2: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/2.jpg)
Delegates
![Page 3: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/3.jpg)
DelegatesHave you ever thought to pass a function as a parameter?
![Page 4: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/4.jpg)
Delegates
• Delegate
– A delegate is an object that points to a function
– is basically a function pointer, C++!
– "Why do I need a reference to a method?"
• The answer boils down to giving you maximum flexibility to implement any functionality you want at
runtime.
– You can use delegate to pass a function as a parameter
![Page 5: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/5.jpg)
Delegate Example
public class MagicNumber{
public int Number { get; set; }}
public void Increment(MagicNumber magicNumber){
magicNumber.Number++;}
public void Decrement(MagicNumber magicNumber){
magicNumber.Number--;}
public delegate void MagicNumberModifier (MagicNumber magicNumber);
Delegate Declaration
![Page 6: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/6.jpg)
Delegate Example
• Now let’s make a list of MagicNumbers
• And a member like this:
List<MagicNumber> magicNumbers = new List<MagicNumber>();
magicNumbers.Add(new MagicNumber() { Number = 4});magicNumbers.Add(new MagicNumber() { Number = 6});magicNumbers.Add(new MagicNumber() { Number = 9});magicNumbers.Add(new MagicNumber() { Number = 13});magicNumbers.Add(new MagicNumber() { Number = 20});
public static void ApplyModifierToAll(MagicNumberModifier modifier, List<MagicNumber> magicNumbers){
foreach(MagicNumber number in magicNumbers){
modifier(number);}
}
Delegate!
public delegate void MagicNumberModifier (MagicNumber magicNumber);
![Page 7: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/7.jpg)
Delegate Example
• Lastly, just call the delegate and let the magic happens
public static void Main(string[] args){
List<MagicNumber> magicNumbers = new List<MagicNumber>();
magicNumbers.Add(new MagicNumber() { Number = 4, IsMagic = true });magicNumbers.Add(new MagicNumber() { Number = 6, IsMagic = false });magicNumbers.Add(new MagicNumber() { Number = 9, IsMagic = true });magicNumbers.Add(new MagicNumber() { Number = 13, IsMagic = false });magicNumbers.Add(new MagicNumber() { Number = 20, IsMagic = true });
ApplyModifierToAll(Increment, magicNumbers);ApplyModifierToAll(Magicify, magicNumbers);
}
![Page 8: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/8.jpg)
MultiCasting-Delegates
• Note!
– If you chain delegates that return values, you may run into some problems. The way C#
handles that situation is by returning the value of only the last function inside the delegate. All
other return values are discarded. So don’t return things that are absolutely vital to your
program if you know the function is going inside a chained delegate.
![Page 9: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/9.jpg)
Event Handling
![Page 10: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/10.jpg)
Event HandlingDelegates and Event Handling
![Page 11: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/11.jpg)
Event HandlingRaise Events when Needed
![Page 12: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/12.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
![Page 13: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/13.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
![Page 14: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/14.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
![Page 15: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/15.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
The event handler
Raising the event
![Page 16: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/16.jpg)
Attaching and Detaching Events
![Page 17: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/17.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
class Program{
public static void Main(){
new Program().Test();}
private void Test(){
Point point = new Point();
// Here's the real important line:point.PointChanged += HandlePointChanged;
// Now if we change the point, the PointChanged event will get raised,// and HandlePointChanged will get called.point.X = 3;
}
public void HandlePointChanged(object sender, EventArgs eventArgs){
// Do something intelligent when the point changes. Perhaps redraw the GUI,// or update another data structure, or anything else you can think of.
}}
![Page 18: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/18.jpg)
Event HandlingHow it works now..
![Page 19: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/19.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
class Program{
public static void Main(){
new Program().Test();}
private void Test(){
Point point = new Point();
// Here's the real important line:point.PointChanged += HandlePointChanged;
// Now if we change the point, the PointChanged event will get raised,// and HandlePointChanged will get called.point.X = 3;
}
public void HandlePointChanged(object sender, EventArgs eventArgs){
// Do something intelligent when the point changes. Perhaps redraw the GUI,// or update another data structure, or anything else you can think of.
}}
When a point is Set the method OnPointChangedwill be called
![Page 20: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/20.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
class Program{
public static void Main(){
new Program().Test();}
private void Test(){
Point point = new Point();
// Here's the real important line:point.PointChanged += HandlePointChanged;
// Now if we change the point, the PointChanged event will get raised,// and HandlePointChanged will get called.point.X = 3;
}
public void HandlePointChanged(object sender, EventArgs eventArgs){
// Do something intelligent when the point changes. Perhaps redraw the GUI,// or update another data structure, or anything else you can think of.
}}
Which will consequently raise the event by calling it (checking if the delegate is null for the first time)
![Page 21: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/21.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
class Program{
public static void Main(){
new Program().Test();}
private void Test(){
Point point = new Point();
// Here's the real important line:point.PointChanged += HandlePointChanged;
// Now if we change the point, the PointChanged event will get raised,// and HandlePointChanged will get called.point.X = 3;
}
public void HandlePointChanged(object sender, EventArgs eventArgs){
// Do something intelligent when the point changes. Perhaps redraw the GUI,// or update another data structure, or anything else you can think of.
}}
And consequently, calling the delegate that points to the programmer function
![Page 22: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/22.jpg)
public class Point{
private double x;private double y;
public double X{
get{
return x;}set{
x = value;OnPointChanged();
}}
public double Y{
get{
return y;}set{
y = value;OnPointChanged();
}}
public event EventHandler PointChanged;
public void OnPointChanged(){
if(PointChanged != null){
PointChanged(this, EventArgs.Empty);}
}}
class Program{
public static void Main(){
new Program().Test();}
private void Test(){
Point point = new Point();
// Here's the real important line:point.PointChanged += HandlePointChanged;
// Now if we change the point, the PointChanged event will get raised,// and HandlePointChanged will get called.point.X = 3;
}
public void HandlePointChanged(object sender, EventArgs eventArgs){
// Do something intelligent when the point changes. Perhaps redraw the GUI,// or update another data structure, or anything else you can think of.
}}
And consequently, calling the delegate that points to the programmer function
![Page 23: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/23.jpg)
Attaching/Detaching Multiple Events
• The event can easily be removed/changed/added at runtime by -= or +=
• An event can also add multiple function pointers (delegates at the same time),
pointing at all of them at the same time and calling all of them when the event is
raised!
point.PointChanged -= HandlePointChanged;
point.PointChanged += HandlePointChanged;point.PointChanged += HandlePotatoPositionChange;
![Page 24: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/24.jpg)
Anonymous Methods
![Page 25: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/25.jpg)
Anonymous Methods
• An anonymous method is a method without a name
– which is why it is called anonymous.
• You don't declare anonymous methods like regular methods.
– Instead they get hooked up directly to events.
![Page 26: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/26.jpg)
Anonymous Methods
using System.Windows.Forms;
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Controls.Add(buttonHello);}
}
![Page 27: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/27.jpg)
Anonymous Methods
using System.Windows.Forms;
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Controls.Add(buttonHello);}
}
![Page 28: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/28.jpg)
Anonymous Methods
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Button btnGoodBye = new Button();btnGoodBye.Text = "Goodbye";btnGoodBye.Left = buttonHello.Width + 5;
btnGoodBye.Click +=delegate(object sender, EventArgs e){
string message = (sender as Button).Text;MessageBox.Show(message);
};
Controls.Add(buttonHello);Controls.Add(btnGoodBye);
}}
![Page 29: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/29.jpg)
Anonymous Methods
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Button btnGoodBye = new Button();btnGoodBye.Text = "Goodbye";btnGoodBye.Left = buttonHello.Width + 5;
btnGoodBye.Click +=delegate(object sender, EventArgs e){
string message = (sender as Button).Text;MessageBox.Show(message);
};
Controls.Add(buttonHello);Controls.Add(btnGoodBye);
}}
![Page 30: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/30.jpg)
Anonymous Methods
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Button btnGoodBye = new Button();btnGoodBye.Text = "Goodbye";btnGoodBye.Left = buttonHello.Width + 5;
btnGoodBye.Click +=delegate(object sender, EventArgs e){
string message = (sender as Button).Text;MessageBox.Show(message);
};
Controls.Add(buttonHello);Controls.Add(btnGoodBye);
}}
![Page 31: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/31.jpg)
Anonymous Methods
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Button btnGoodBye = new Button();btnGoodBye.Text = "Goodbye";btnGoodBye.Left = buttonHello.Width + 5;
btnGoodBye.Click +=delegate(object sender, EventArgs e){
string message = (sender as Button).Text;MessageBox.Show(message);
};
Controls.Add(buttonHello);Controls.Add(btnGoodBye);
}}
![Page 32: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/32.jpg)
Anonymous Methods
public partial class Form1 : Form{
public Form1(){
Button buttonHello = new Button();buttonHello.Text = "Hello";
buttonHello.Click +=delegate{
MessageBox.Show("Hello");};
Button btnGoodBye = new Button();btnGoodBye.Text = "Goodbye";btnGoodBye.Left = buttonHello.Width + 5;
btnGoodBye.Click +=delegate(object sender, EventArgs e){
string message = (sender as Button).Text;MessageBox.Show(message);
};
Controls.Add(buttonHello);Controls.Add(btnGoodBye);
}}
![Page 33: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/33.jpg)
Extension Methods
![Page 34: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/34.jpg)
Extension MethodsExtension methods are a way to create a method that feels like it is part of a specific class
(like, the string class) when you don't actually have access to that class to modify, and add the method.
![Page 35: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/35.jpg)
Extension MethodsIt’s a way to add functionalities to classes you don’t own
![Page 36: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/36.jpg)
Extension MethodsIt’s a way to add functionalities to classes you don’t own
that’s why they are called Extension Methods
![Page 37: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/37.jpg)
Extension Methods
• Extension methods must be added in static classes (where methods in that static
class should also be static)
• Now, Let’s extend the String class with a ToRandomCase method that
randomize the letter cases in a given string.
![Page 38: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/38.jpg)
Extension Methodspublic static class StringExtensions
{private static Random random = new Random();
public static string ToRandomCase(this string text){
string result = "";
for (int index = 0; index < text.Length; index++){
if (random.Next(2) == 0){
result += text.Substring(index, 1).ToUpper();}else{
result += text.Substring(index, 1).ToLower();}
}
return result;}
}
Static class
![Page 39: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/39.jpg)
Extension Methodspublic static class StringExtensions
{private static Random random = new Random();
public static string ToRandomCase(this string text){
string result = "";
for (int index = 0; index < text.Length; index++){
if (random.Next(2) == 0){
result += text.Substring(index, 1).ToUpper();}else{
result += text.Substring(index, 1).ToLower();}
}
return result;}
}
Static data member
![Page 40: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/40.jpg)
Extension Methodspublic static class StringExtensions
{private static Random random = new Random();
public static string ToRandomCase(this string text){
string result = "";
for (int index = 0; index < text.Length; index++){
if (random.Next(2) == 0){
result += text.Substring(index, 1).ToUpper();}else{
result += text.Substring(index, 1).ToLower();}
}
return result;}
}
Static method
![Page 41: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/41.jpg)
Extension Methodspublic static class StringExtensions
{private static Random random = new Random();
public static string ToRandomCase(this string text){
string result = "";
for (int index = 0; index < text.Length; index++){
if (random.Next(2) == 0){
result += text.Substring(index, 1).ToUpper();}else{
result += text.Substring(index, 1).ToLower();}
}
return result;}
}
The extended class (string)
with the this keyword
![Page 42: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/42.jpg)
Extension Methodspublic static class StringExtensions
{private static Random random = new Random();
public static string ToRandomCase(this string text){
string result = "";
for (int index = 0; index < text.Length; index++){
if (random.Next(2) == 0){
result += text.Substring(index, 1).ToUpper();}else{
result += text.Substring(index, 1).ToLower();}
}
return result;}
}
A simple randomization
algorithm
![Page 43: C# Starter L06-Delegates, Event Handling and Extension Methods](https://reader030.fdocuments.net/reader030/viewer/2022020101/5553b440b4c905d9448b4c54/html5/thumbnails/43.jpg)
Extension Methods
• Now just call the method!
public static void Main(){
string message = "Do you wish me a good morning, or mean that it is a good morning whether I want it or not; or that you feel good this morning; or that it is a morning to be good on?";
Console.WriteLine(message.ToRandomCase());}
Do You WisH mE A gOOD MOrniNG, Or mEan that it iS a GOOD moRnIng wHether i Want It or noT; Or that yOufeeL goOD THiS morning; OR tHaT it iS A MOrNINg to be GoOd On?Press any key to continue . . .