Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface.
For instance, you might like to know whether a certain string was a number or not. The usual approach would be to define a function and then call it each time, and once you got a whole lot of those kind of functions, you would put them together in a utility class, like this:
public class MyUtils
{
public static bool IsNumeric(string s)
{
float output;
return float.TryParse(s, out output);
}
}
Now we check string by executive above code like below
string test = "4";
if (MyUtils.IsNumeric(test))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
Note : However, with Extension Methods, you can actually extend the String class to support this directly. You do it by defining a static class, with a set of static methods that will be your library of extension methods. Here is an example:
The IsNumeric() method is not a method of string data type . It is an extension method written by the programmer for the string data type. The IsNumeric() extension method will be available throughout the application by including the namespace in which it has been defined.
public static class MyExtensionMethods
{
public static bool IsNumeric(this string s)
{
float output;
return float.TryParse(s, out output);
}
}
The only thing that separates this from any other static method, is the "this" keyword in the parameter section of the method. It tells the compiler that this is an extension method for the string class, and that's actually all you need to create an extension method. Now, you can call the IsNumeric() method directly on strings, like this:
string test = "4";
if (test.IsNumeric())
Console.WriteLine("Yes");
else
Console.WriteLine("No");
Note : The only difference between a regular static method and an extension method is that the first parameter of the extension method specifies the type that it is going to operator on, preceded by the this keyword.
Please see below screen shoot .
For instance, you might like to know whether a certain string was a number or not. The usual approach would be to define a function and then call it each time, and once you got a whole lot of those kind of functions, you would put them together in a utility class, like this:
public class MyUtils
{
public static bool IsNumeric(string s)
{
float output;
return float.TryParse(s, out output);
}
}
Now we check string by executive above code like below
string test = "4";
if (MyUtils.IsNumeric(test))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
Note : However, with Extension Methods, you can actually extend the String class to support this directly. You do it by defining a static class, with a set of static methods that will be your library of extension methods. Here is an example:
The IsNumeric() method is not a method of string data type . It is an extension method written by the programmer for the string data type. The IsNumeric() extension method will be available throughout the application by including the namespace in which it has been defined.
public static class MyExtensionMethods
{
public static bool IsNumeric(this string s)
{
float output;
return float.TryParse(s, out output);
}
}
The only thing that separates this from any other static method, is the "this" keyword in the parameter section of the method. It tells the compiler that this is an extension method for the string class, and that's actually all you need to create an extension method. Now, you can call the IsNumeric() method directly on strings, like this:
string test = "4";
if (test.IsNumeric())
Console.WriteLine("Yes");
else
Console.WriteLine("No");
Note : The only difference between a regular static method and an extension method is that the first parameter of the extension method specifies the type that it is going to operator on, preceded by the this keyword.
Please see below screen shoot .
No comments:
Post a Comment