Reputation: 311
What is the difference between generic method and generic extension method and extension method?
Upvotes: 3
Views: 1401
Reputation: 3573
Generic method by MSDN.
A generic method is a method that is declared with type parameters
static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
This method swaps the references between lhs (left-hand-side) and rhs (right-hand-side). Because we only want to swap the references and don't care about what the underlying types of the references are, we can declare the method as a generic method with type parameter T. This means it can be of any type. This saves us from having to write multiple Swap methods.
string s1 = "hello";
string s2 = "world";
Swap(ref s1, ref s2);
int i1 = 5;
int i2 = 12;
Swap(ref i1, ref i2);
While the example could be written using object types as Swap method parameters, this would cause an unnecessary overhead with value types known as boxing.
Extension method by MSDN
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
Let's say we want to extend the existing string class to contain a method for counting words in the string.
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
Now we can count words from any string object.
string s = "Hello Extension Methods";
int i = s.WordCount();
This is especially useful for adding features (methods) to existing classes which you do not have access to (from third party assembly, for example).
Generic extension methods are simply a mix of the previous two concepts.
Upvotes: 3
Reputation: 1451
Methods can be generic or nongeneric, for example:
public void Foo() { } // Non-Generic Method
public void Foo<T>(T value) { } // Generic Method
Extension methods are methods used to extend the behavior of types without modifying the type itself. Say you want the String
type to have a Reverse
method, you could define an extension method on the String type, like this:
public static class ExtMethods
{
public static string Reverse(this string s) // Non-Generic Extension Method
{
// code to reverse string
}
}
Extension methods must be declared static and within a static class, also its first parameter must have this
before the type it extends.
Likewise, extension methods can be generic:
public static class ExtMethods
{
public static Foo<T>(this T obj) // Generic extension method
{
}
}
So, a Generic Extension Method is just an extension method that happens to be generic.
Upvotes: 0
Reputation: 311
• Extension Method: With extension method can add some extra method to specified type. For create extension method
- Definition class with public static attribute.
- Definition method in class with public static attribute.
- For first parameter of method defined extension method .place before this parameter keyword this.
public static class TestExtensionClass
{
public static string TestExtinsionMethod(this string password)
{
string encriptedPassword="";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(password);
foreach (byte b in ASCIIValues)
{
encriptedPassword += b.ToString();
}
return encriptedPassword;
}
}
- In other classes call extension method.
private void CallExtensionMethod()
{
string s = "123";
s.TestExtinsionMethod();
}
• Generic Method: With generic method you can define output type in runtime. For create extension method
Definition class.
Definition method. Before name of method place T.
- After name of method place <**T>**.
public T TestCastTo<T>(object obj)
{
return (T)obj;
}
- In other classes call Generic method.
public static T TestCastTo<T>(this object obj)
{
return (T)obj;
}
• Generic Extension Method: • With combine properties Extension Method and Generic Method you can gain a Generic Extension Method.
public static T TestCastTo<T>(this object obj)
{
return (T)obj;
}
in other class call generic extension method
private void CallGenericExtensionMethod()
{
string s = "123";
int i = s.TestCastTo<int>();
}
Upvotes: 0
Reputation: 5973
generic method is called just like regular method, with the difference it can be used for different types by specifying generic type.
someObject.GenericMethodFromSameClass<String>();
generic extension method and extension method are similar to each other in a sense that they can be called on objects they extend. Difference between them is the same as difference between regular method and generic method.
someObject.ExtensionMethodFromOtherClass();
someObject.GenericExtensionMethodFromOtherClass<String>();
Upvotes: 1