-2

I found this code here. I know its basics, but what does the 'this Type type' in the method parameters do, could sombody please explain?

public static bool InheritsFrom(this Type type, Type baseType)
    {
    // null does not have base type
    if (type == null)
    {
        return false;
    }

    // only interface can have null base type
    if (baseType == null)
    {
        return type.IsInterface;
    }

    // check implemented interfaces
    if (baseType.IsInterface)
    {
        return type.GetInterfaces().Contains(baseType);
    }

    // check all base types
    var currentType = type;
    while (currentType != null)
    {
        if (currentType.BaseType == baseType)
        {
            return true;
        }

        currentType = currentType.BaseType;
    }

        return false;
}
majorTom
  • 57
  • 6

1 Answers1

-5

its for the extension method , in C# there is concept called Extension method , that syntax is for extension methods

What is Extesion Methods ?

Method allow programmer to "add" methods to existing types without creating a new derived type, recompiling, or by modifying the original type. Methods are static methods they are called as if they were instance methods on the extended type.

Example :

public static class Utilities
{
    public static string encryptString(this string str)
    {
           System.Security.Cryptography.MD5CryptoServiceProvider x = new      System.Security.Cryptography.MD5CryptoServiceProvider();
           byte[] data = System.Text.Encoding.ASCII.GetBytes(str);
           data = x.ComputeHash(data);

           return System.Text.Encoding.ASCII.GetString(data);
     }
}

this extends string type and add entryptString method on string type.

check here for more : Extension Methods

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263