In this video
will try to understand
what exactly are a extension methods
and in what scenarios we will be using extension methods
extension methods
help you to add methods to existing types
without creating new derive types
or modifying the original type
I have created a very simple console application here
and this console application name is
maths program
this maths program application
helps you to do arithmetic calculation like addition, multiplication, division etc
This maths program
is a console application
We will be using a third party component to do our addition and subtraction
We have a very simple third party component called as maths.dll
This console application will be using this
maths.dll to achieve
the arithmetic calculation
We will copy this
thing over here will go and add a reference here
of this
Third party component
Browse to that folder
In order to use this maths program I will import the
maths namespace
I can start creating the object of the maths class to do
my arithmetic calculation
If I do addition I can use this add method here
and I hope that it will display me 20
I can say console.readline
If I run this
I should see the value
20
This third party component that is maths.dll is working perfectly well
It is doing addition of
two numbers and also displaying the same
We would also like to do subtractions
This third party dll
has a add function but he does not have
a subtract function
The third party dll is not supporting the subtract function
Open this
third party maths components source code
add a new function called as subtract
recompile it, rerefrence a dll
and then try to use a subtract function
This is a third party component
The third party is not willing to share the source code with you
Whatever dll you have for now is what you have
this solution over here is practically not possible because he don’t have the
source code of the third party
The other solution is how about creating one more class library
and then
that class library will go and wrap this maths third party class library inside it
and he will have a subtract function
as well as he will have a add function
Create some kind of a class library here called as
advance maths or
Wrapper maths
I will reference that
dll
I can create
a public object
of the
third party maths
I can add a
subtract function also here
public
int
subtract
int num1, int
num2
num1
minus num2
This is one we dealing this problem
In my client I can add this add to reference
to this wrapper component here
I can use this
wrapper maths
Also imort the namespace
using wrapper maths
I can create the object of some more maths class
If we want to call the add function
If we want to call the subtraction function
Whenever we want to invoke the add function, will invoke this add function called as MathsThirdParty
The subtract function will be from here
We can achieve the same thing by using inheritance as well
SomeMoreMaths classes using Aggregation
or composition
We can inherit this MathsThirdParty and do the subtraction
You either go by inheritance or either go by aggregation the way I have shown
We end up with a complicated class object
If you want to call Add
Use this MathsThirdParty class
If you want to call Subtract
use SomeMoreMaths class
The time he creates MathsThirdParty
If he able to see Subtract here itself
in the MathsThirdParty
That can be achieved by using Extension methods
Extension method helps to ad
methods to a class
without changing the original source code
We cannot change this third party source code
We don't need to aggregate or inherit that class
Create static class
We have provided this class SomeMoreMaths
We don't want to extend the ThirdPartyMaths
If we do a dot(.)
the Subtract function has come over there
This Subtract function
is
become a part of your MathsThirdParty
At the left hand side of this Subtract word there is a blue arrow which indicate this is a extension method
This Add is complete pink
Its a normal method
By using extension method we have neither changing the class name
We kept the class structure simple as it was previously
We have added this Subtract functionality over here
The person who will use this component
does not have to worry that he is using a MathsThirdParty or he is using some other Maths class
He has to use the MathsThirdParty
and will get all the Add function and Subtract function
Hope you enjoyed this video
In this video were trying to understand what exactly are extension methods
In what scenarios we should be using extension