Extension Method in C#

Extension Method in C#
Qst 1. What is Extension method.
Ans. An extension method is a static method of a static class that can be invoked like as an instance method syntax. Extension methods are used to add new behaviors to an existing type without altering it.
What does it mean it means extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
In extension method "this" keyword is used with the first parameter and the first parameter will be of type that is extended by extension method.
The most common extension methods are the LINQ standard query operators that add query functionality to the existing on  IEnumerable  and IQeryable type, for an example.


using System;
using ExtensionMethod; //  to access extension method
namespace MainProgramm
{
    class MainProgram
    {
        static void Main(string[] args)
        {
            DateTime CurrentDate = DateTime.Now;
            Console.WriteLine("Current Date : " + CurrentDate);
            string  FormatedDate  = CurrentDate.DMYhmsString();
            Console.WriteLine("Formated Date : " + FormatedDate);
            Console.ReadLine();
        }     
    }
}.
using System;
namespace ExtensionMethod
{
    public static class ExtensionMethod
    {
        public static string  DMYhmsString(this DateTime AttendDate)
        {
            return AttendDate.ToString("dd MMM yyyy hh:mm:ss");
        }
    }
}

Output.






Qst 2. What is advantages of using Extension method.
Ans.  There is no need to creating a new derived type, recompiling, or otherwise modifying the original type.

Qst 3Extension method is a static method of static class but it call as extension method can you explain why encapsulation rule not violated.
Ans. The intermediate language (IL) generated by the compiler translates your code into a call on the static method. The principle of encapsulation is not really being violated.\
here is the MSIL code of above example.



Qst 4. Can extension methods access private variables in the type they are extending?.
Ans. No, Cause inaccessible due to protection level.









Qst 5. Will  an extension method  be call if it has the same signature as a method defined in the type?.
Ans. An extension method will never be called if it has the same signature as a method defined in the type. At compile time, extension methods always have lower priority than instance methods defined in the type itself. Hence you can use extension methods to extend a class or interface, but not to override them.
Let see an example for better understanding.





















Output. you can see in extension method we have printed some label but not come in output since ToLower() method already exists in string type .








Qst 5. Can I add extension methods to an existing static class?.
Ans. No. Cause Instance with this keyword is going to extend and we know static class does not allow this or instance.
Qst 5. Can I add ref and out keyword with first parameter extension methods?.
Ans. No.  
 






  SQL interview Question Answer- Part -  2 Question 16. What is an Index? Answer :  Indexes are used to retrieval data more quickly from dat...