SlideShare a Scribd company logo
Mohammad Shaker
mohammadshaker.com
C# Programming Course
@ZGTRShaker
2011, 2012, 2013, 2014
C# Starter
L06 – Delegates, Event Handling and
Extension Methods
Delegates
Delegates
Have you ever thought to pass a function as a parameter?
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
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
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);
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);
}
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.
Event Handling
Event Handling
Delegates and Event Handling
Event Handling
Raise Events when Needed
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);
}
}
}
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);
}
}
}
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);
}
}
}
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
Attaching and Detaching Events
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.
}
}
Event Handling
How it works now..
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 OnPointChanged
will be called
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)
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
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
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;
Anonymous Methods
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.
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Extension Methods
Extension Methods
Extension 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.
Extension Methods
It’s a way to add functionalities to classes you don’t own
Extension Methods
It’s a way to add functionalities to classes you don’t own
that’s why they are called Extension Methods
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.
Extension Methods
public 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
Extension Methods
public 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
Extension Methods
public 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
Extension Methods
public 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
Extension Methods
public 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
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 yOu
feeL goOD THiS morning; OR tHaT it iS A MOrNINg to be GoOd On?
Press any key to continue . . .

More Related Content

What's hot

04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
rohit_gupta_mrt
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
hhliu
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Flying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightFlying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnight
Wiem Zine Elabidine
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Reviewing a Complex DataWeave Transformation Use-case v2
Reviewing a Complex DataWeave Transformation Use-case v2Reviewing a Complex DataWeave Transformation Use-case v2
Reviewing a Complex DataWeave Transformation Use-case v2
Alexandra N. Martinez
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
Bhavik Vashi
 
Antenna Physical Characetristics
Antenna Physical CharacetristicsAntenna Physical Characetristics
Antenna Physical CharacetristicsAssignmentpedia
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
Ali Parmaksiz
 
Pure Future
Pure FuturePure Future
Pure Future
Wiem Zine Elabidine
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesConverting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesKaniska Mandal
 
Reviewing a complex dataweave transformation use case v3
Reviewing a complex dataweave transformation use case v3Reviewing a complex dataweave transformation use case v3
Reviewing a complex dataweave transformation use case v3
Alexandra N. Martinez
 
Ip project visual mobile
Ip project visual mobileIp project visual mobile
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Codemotion
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
Chris Eargle
 
Advanced JavaScript Concepts
Advanced JavaScript ConceptsAdvanced JavaScript Concepts
Advanced JavaScript Concepts
Naresh Kumar
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Ontico
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 

What's hot (20)

04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Flying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightFlying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnight
 
JavaProgrammingManual
JavaProgrammingManualJavaProgrammingManual
JavaProgrammingManual
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Reviewing a Complex DataWeave Transformation Use-case v2
Reviewing a Complex DataWeave Transformation Use-case v2Reviewing a Complex DataWeave Transformation Use-case v2
Reviewing a Complex DataWeave Transformation Use-case v2
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Antenna Physical Characetristics
Antenna Physical CharacetristicsAntenna Physical Characetristics
Antenna Physical Characetristics
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo A GWT Application with MVP Pattern Deploying to CloudFoundry using  Spring Roo
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
 
Pure Future
Pure FuturePure Future
Pure Future
 
Converting Db Schema Into Uml Classes
Converting Db Schema Into Uml ClassesConverting Db Schema Into Uml Classes
Converting Db Schema Into Uml Classes
 
Reviewing a complex dataweave transformation use case v3
Reviewing a complex dataweave transformation use case v3Reviewing a complex dataweave transformation use case v3
Reviewing a complex dataweave transformation use case v3
 
Ip project visual mobile
Ip project visual mobileIp project visual mobile
Ip project visual mobile
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
 
Advanced JavaScript Concepts
Advanced JavaScript ConceptsAdvanced JavaScript Concepts
Advanced JavaScript Concepts
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Viewers also liked

C# Starter L07-Objects Cloning
C# Starter L07-Objects CloningC# Starter L07-Objects Cloning
C# Starter L07-Objects Cloning
Mohammad Shaker
 
XNA L11–Shaders Part 2
XNA L11–Shaders Part 2XNA L11–Shaders Part 2
XNA L11–Shaders Part 2Mohammad Shaker
 
C# Advanced L09-HTML5+ASP
C# Advanced L09-HTML5+ASPC# Advanced L09-HTML5+ASP
C# Advanced L09-HTML5+ASP
Mohammad Shaker
 
XNA L10–Shaders Part 1
XNA L10–Shaders Part 1XNA L10–Shaders Part 1
XNA L10–Shaders Part 1Mohammad Shaker
 
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
Utilizing Kinect Control for a More Immersive Interaction with 3D EnvironmentUtilizing Kinect Control for a More Immersive Interaction with 3D Environment
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
Mohammad Shaker
 
Indie Series 03: Becoming an Indie
Indie Series 03: Becoming an IndieIndie Series 03: Becoming an Indie
Indie Series 03: Becoming an Indie
Mohammad Shaker
 
WPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D AnimationWPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D AnimationMohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 
C# Advanced L10-Workflow Foundation
C# Advanced L10-Workflow FoundationC# Advanced L10-Workflow Foundation
C# Advanced L10-Workflow Foundation
Mohammad Shaker
 
WPF L01-Layouts, Controls, Styles and Templates
WPF L01-Layouts, Controls, Styles and TemplatesWPF L01-Layouts, Controls, Styles and Templates
WPF L01-Layouts, Controls, Styles and TemplatesMohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
C# Advanced L08-Networking+WCF
C# Advanced L08-Networking+WCFC# Advanced L08-Networking+WCF
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and eventsIblesoft
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
OpenGL Starter L02
OpenGL Starter L02OpenGL Starter L02
OpenGL Starter L02
Mohammad Shaker
 
Car Dynamics with ABS, ESP and GPS Systems
Car Dynamics with ABS, ESP and GPS SystemsCar Dynamics with ABS, ESP and GPS Systems
Car Dynamics with ABS, ESP and GPS Systems
Mohammad Shaker
 
[Development Simply Put] Events & Delegates In C# - Win Forms Controls
[Development Simply Put] Events & Delegates In C# - Win Forms Controls[Development Simply Put] Events & Delegates In C# - Win Forms Controls
[Development Simply Put] Events & Delegates In C# - Win Forms Controls
Ahmed Tarek Hasan
 
c# events, delegates and lambdas
c# events, delegates and lambdasc# events, delegates and lambdas
c# events, delegates and lambdasFernando Galvan
 
12 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_1712 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_17Niit Care
 

Viewers also liked (20)

C# Starter L07-Objects Cloning
C# Starter L07-Objects CloningC# Starter L07-Objects Cloning
C# Starter L07-Objects Cloning
 
XNA L11–Shaders Part 2
XNA L11–Shaders Part 2XNA L11–Shaders Part 2
XNA L11–Shaders Part 2
 
C# Advanced L09-HTML5+ASP
C# Advanced L09-HTML5+ASPC# Advanced L09-HTML5+ASP
C# Advanced L09-HTML5+ASP
 
XNA L10–Shaders Part 1
XNA L10–Shaders Part 1XNA L10–Shaders Part 1
XNA L10–Shaders Part 1
 
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
Utilizing Kinect Control for a More Immersive Interaction with 3D EnvironmentUtilizing Kinect Control for a More Immersive Interaction with 3D Environment
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
 
Indie Series 03: Becoming an Indie
Indie Series 03: Becoming an IndieIndie Series 03: Becoming an Indie
Indie Series 03: Becoming an Indie
 
Delphi L02 Controls P1
Delphi L02 Controls P1Delphi L02 Controls P1
Delphi L02 Controls P1
 
WPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D AnimationWPF L03-3D Rendering and 3D Animation
WPF L03-3D Rendering and 3D Animation
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 
C# Advanced L10-Workflow Foundation
C# Advanced L10-Workflow FoundationC# Advanced L10-Workflow Foundation
C# Advanced L10-Workflow Foundation
 
WPF L01-Layouts, Controls, Styles and Templates
WPF L01-Layouts, Controls, Styles and TemplatesWPF L01-Layouts, Controls, Styles and Templates
WPF L01-Layouts, Controls, Styles and Templates
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
C# Advanced L08-Networking+WCF
C# Advanced L08-Networking+WCFC# Advanced L08-Networking+WCF
C# Advanced L08-Networking+WCF
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
OpenGL Starter L02
OpenGL Starter L02OpenGL Starter L02
OpenGL Starter L02
 
Car Dynamics with ABS, ESP and GPS Systems
Car Dynamics with ABS, ESP and GPS SystemsCar Dynamics with ABS, ESP and GPS Systems
Car Dynamics with ABS, ESP and GPS Systems
 
[Development Simply Put] Events & Delegates In C# - Win Forms Controls
[Development Simply Put] Events & Delegates In C# - Win Forms Controls[Development Simply Put] Events & Delegates In C# - Win Forms Controls
[Development Simply Put] Events & Delegates In C# - Win Forms Controls
 
c# events, delegates and lambdas
c# events, delegates and lambdasc# events, delegates and lambdas
c# events, delegates and lambdas
 
12 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_1712 iec t1_s1_oo_ps_session_17
12 iec t1_s1_oo_ps_session_17
 

Similar to C# Starter L06-Delegates, Event Handling and Extension Methods

This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
indiaartz
 
Functions
FunctionsFunctions
Functions
Jesmin Akhter
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
RichardWarburton
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your codevmandrychenko
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programming
BryceLohr
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan1985
 
Bindings: the zen of montage
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montageKris Kowal
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
Mohammad Shaker
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
RichardWarburton
 
Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014
hezamu
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
Thesis Scientist Private Limited
 
Refactoring
RefactoringRefactoring
Refactoring
Tausun Akhtary
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
Vadym Khondar
 
JavaScript Patterns to Cleanup your Code
JavaScript Patterns to Cleanup your CodeJavaScript Patterns to Cleanup your Code
JavaScript Patterns to Cleanup your Code
Dan Wahlin
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
2017eee0459
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
Matthias Noback
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 

Similar to C# Starter L06-Delegates, Event Handling and Extension Methods (20)

This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 
Functions
FunctionsFunctions
Functions
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your code
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programming
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Bindings: the zen of montage
Bindings: the zen of montageBindings: the zen of montage
Bindings: the zen of montage
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
 
Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Refactoring
RefactoringRefactoring
Refactoring
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
JavaScript Patterns to Cleanup your Code
JavaScript Patterns to Cleanup your CodeJavaScript Patterns to Cleanup your Code
JavaScript Patterns to Cleanup your Code
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
DPC 2019, Amsterdam: Beyond design patterns and principles - writing good OO ...
 
C++ Function
C++ FunctionC++ Function
C++ Function
 

More from Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
Mohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 
Indie Series 01: Intro to Games
Indie Series 01: Intro to GamesIndie Series 01: Intro to Games
Indie Series 01: Intro to Games
Mohammad Shaker
 
Indie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSevenIndie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSeven
Mohammad Shaker
 
Indie Series 02: AI and Recent Advances in Games
Indie Series 02: AI and Recent Advances in GamesIndie Series 02: AI and Recent Advances in Games
Indie Series 02: AI and Recent Advances in Games
Mohammad Shaker
 

More from Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Indie Series 01: Intro to Games
Indie Series 01: Intro to GamesIndie Series 01: Intro to Games
Indie Series 01: Intro to Games
 
Indie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSevenIndie Series 04: The Making of SyncSeven
Indie Series 04: The Making of SyncSeven
 
Indie Series 02: AI and Recent Advances in Games
Indie Series 02: AI and Recent Advances in GamesIndie Series 02: AI and Recent Advances in Games
Indie Series 02: AI and Recent Advances in Games
 

Recently uploaded

Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 

Recently uploaded (20)

Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 

C# Starter L06-Delegates, Event Handling and Extension Methods

  • 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L06 – Delegates, Event Handling and Extension Methods
  • 3. Delegates Have you ever thought to pass a function as a parameter?
  • 4. 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
  • 5. 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
  • 6. 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);
  • 7. 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); }
  • 8. 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.
  • 12. 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); } } }
  • 13. 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); } } }
  • 14. 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); } } }
  • 15. 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
  • 17. 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. } }
  • 18. Event Handling How it works now..
  • 19. 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 OnPointChanged will be called
  • 20. 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)
  • 21. 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
  • 22. 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
  • 23. 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;
  • 25. 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.
  • 26. 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); } }
  • 27. 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); } }
  • 28. 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); } }
  • 29. 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); } }
  • 30. 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); } }
  • 31. 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); } }
  • 32. 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); } }
  • 34. Extension Methods Extension 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.
  • 35. Extension Methods It’s a way to add functionalities to classes you don’t own
  • 36. Extension Methods It’s a way to add functionalities to classes you don’t own that’s why they are called Extension Methods
  • 37. 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.
  • 38. Extension Methods public 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
  • 39. Extension Methods public 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
  • 40. Extension Methods public 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
  • 41. Extension Methods public 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
  • 42. Extension Methods public 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
  • 43. 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 yOu feeL goOD THiS morning; OR tHaT it iS A MOrNINg to be GoOd On? Press any key to continue . . .