1.

What are extension methods and where can we use them?

Answer»

EXTENSION methods allow you to add our own custom method in any custom class without modifying into it and in our custom class and it will be available throughout the application by SPECIFYING namespace only where it is defined.

Example: 

namespace ExtensionMethod {    public STATIC class IntExtension     {        public static bool IsGreaterThan(this int i, int value)        {            return i > value;        }    } }

Here the first parameter specifies the type on which the extension method is APPLICABLE. We are going to use this extension method on int type. So the first parameter MUST be int preceded with the modifier.

using ExtensionMethod; class Program {    static void Main(string[] args)    {        int i = 10;        bool result = i.IsGreaterThan(100);        Console.WriteLine(result);    } }

Output:

False


Discussion

No Comment Found