This is done with the help of Extension Method.
Now we need to extend the definition of this class so m going to create a static class to create an extinction method like:
Output will be:
Other Example:
using System;
using cn2;
using cn3;
namespace cn1
{
class Program
{
static void Main(string[] args)
{
Employee1 emp = new Employee1();
emp.Method1();
emp.EntensionMethod1();
Console.Read();
}
}
}
namespace cn2
{
class Employee1
{
public void Method1()
{
Console.WriteLine("This is Employee1 class Method");
}
}
}
namespace cn3
{
static class Employee2
{
public static void EntensionMethod1(this Employee1 emp)
{
Console.WriteLine("This is extension metod");
}
}
}
Extension Method. Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.
How to use extension methods?
An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
Like: suppose we have a class like bellow:
- public class Class1 {
- public string Display() {
- return ("I m in Display");
- }
- public string Print() {
- return ("I m in Print");
- }
- }
- public static class XX {
- public static void NewMethod(this Class1 ob) {
- Console.WriteLine("Hello I m extended method");
- }
- }
Here I just create a method that name is NewMethod with a parameter using this to define which type of data I need to be extend, now let’s see how to use this function.
- class Program {
- static void Main(string[] args) {
- Class1 ob = new Class1();
- ob.Display();
- ob.Print();
- ob.NewMethod();
- Console.ReadKey();
- }
- }
Other Example:
using System;
using cn2;
using cn3;
namespace cn1
{
class Program
{
static void Main(string[] args)
{
Employee1 emp = new Employee1();
emp.Method1();
emp.EntensionMethod1();
Console.Read();
}
}
}
namespace cn2
{
class Employee1
{
public void Method1()
{
Console.WriteLine("This is Employee1 class Method");
}
}
}
namespace cn3
{
static class Employee2
{
public static void EntensionMethod1(this Employee1 emp)
{
Console.WriteLine("This is extension metod");
}
}
}
Benefits of extension methods
- Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code.
- If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.
- This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class's design.
Important points for the use of extension methods:
- An extension method must be defined in a top-level static class.
- An extension method with the same name and signature as an instance method will not be called.
- Extension methods cannot be used to override existing methods.
- The concept of extension methods cannot be applied to fields, properties or events.
- Overuse of extension methods is not a good style of programming.
No comments:
Post a Comment