What is the difference between generic method and generic extension method and extension method?

What is the difference between generic method and generic extension method and extension method ?

+3


source to share


4 answers


General method on MSDN.

A generic method is a method declared with type parameters

static void Swap<T>(ref T lhs, ref T rhs)
{
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;
}

      

This method replaces links between lhs (left side) and rhs (right side). Since we only want to exchange references and don't care what the basic reference types are, we can declare this method as a generic method with a 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 parameters to the Swap method, this would result in unnecessary overhead with values ​​known as boxing.


MSDN extension method



Extension methods allow you to "add" methods to existing types without creating a new derived type, recompiling, or modifying the original type.

Let's say we want to extend an existing string class to contain a method for counting words in a string.

public static int WordCount(this String str)
{
    return str.Split(new char[] { ' ', '.', '?' }, 
                     StringSplitOptions.RemoveEmptyEntries).Length;
}

      

Now we can read words from any string object.

string s = "Hello Extension Methods";
int i = s.WordCount();

      

This is especially useful for adding functions (methods) to existing classes that you don't have access to (for example, from a third party assembly).


Generic extension methods are simply a combination of the two previous concepts.

+3


source


a generic method is called in the same way as a normal method, with the difference that it can be used for different types by specifying the generic type.

someObject.GenericMethodFromSameClass<String>();

      



a generic extension method and an extension method are similar to each other in the sense that they can be called on the objects they propagate. The difference between them is the same as the difference between the conventional method and the general method.

someObject.ExtensionMethodFromOtherClass();
someObject.GenericExtensionMethodFromOtherClass<String>();

      

+1


source


• Extension Method: An extension method can be used to add an additional method to a specified type . To create an extension method

  • A definition class with a public static attribute .
  • A definition method in a class with a public static attribute .
  • For the first parameter of a method defined by an extension method. Place up to this keyword of 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;
    }
}

      

  1. The extension method is called in other classes.
       private void CallExtensionMethod()
    {
        string s = "123";
        s.TestExtinsionMethod();
    }

      

• Generic method: With a generic method, you can determine the type of output at runtime. To create an extension method

  • Definition class.

  • Determination method. Place T in front of the method name .

  • After the method name <** T > **.
     public T TestCastTo<T>(object obj)
    {
        return (T)obj;
    }

      

  1. Other classes call Generic.
     public static T TestCastTo<T>(this object obj)
    {
        return (T)obj;
    }

      

• Generic extension method: • Combined with an extension method and a generic method, you can get a generic extension method.

     public static T TestCastTo<T>(this object obj)
    {
        return (T)obj;
    }

      

in a common class extension method

    private void CallGenericExtensionMethod()
    {
        string s = "123";
        int i = s.TestCastTo<int>();
    }

      

0


source


The methods can be general or non-viable, 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 changing the type itself. Let's say you want a type to String

have a method Reverse

, you could define an extension method for a String type, like so:

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 in a static class, and also have its first parameter this

before the type that 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 is generic.

0


source







All Articles