Sunday, August 7, 2016

Extension methods

allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface. 
Extension methods can be added to your own custom class, .NET framework classes, or third party classes or interfaces.
E.g. The IsGreaterThan() method is not a method of int data type (Int32 struct). It is an extension method written by the programmer for the int data type.
int i = 10;
bool result = i.IsGreaterThan(100);

    public static class IntExtensions
     {
        public static bool IsGreaterThan(this int i, int value)
        {
            return i > value;
        }
    }

No comments:

Post a Comment