jagomart
digital resources
picture1_C Sharp Extension Method


 155x       Filetype PDF       File size 0.26 MB       Source: www.idc-online.com


File: C Sharp Extension Method
c sharp extension method an extension method is a static method of a static class that can be invoked using the instance method syntax extension methods are used to add ...

icon picture PDF Filetype PDF | Posted on 01 Feb 2023 | 2 years ago
Partial capture of text on file.
                   C SHARP EXTENSION METHOD 
          
         An extension method is a static method of a static class that can be invoked using the instance method 
         syntax.  Extension  methods  are  used  to  add  new  behaviors  to  an  existing  type  without  altering.  In 
         extension method "this" keyword is used with the first parameter and the type of the first parameter will 
         be the type that is extended by extension method. 
         Conceptually, extension methods provides as an implementation of the decorator structural pattern. At 
         compile time an extension method call is translated into an ordinary static method call. 
         A simple Extension Method Example 
             //defining extension method
           1.                     
             
           2. public static class MyExtensions 
             
           3. { 
             
           4.  public static int WordCount(this String str) 
             
           5.  { 
             
           6.  return str.Split(new char[] { ' ', '.', ',' }).Length; 
             
           7.  } 
             
           8. }  
             
           9.   
             
           10. class Program 
              
           11. { 
              
           12.  public static void Main() 
              
           13.  { 
              
           14.  string s = "Dot Net Tricks Extension Method Example"; 
              
           15.   
              
               //calling extension method
           16.                     
              
           17.  int i = s.WordCount(); 
              
           18.   
              
           19.  Console.WriteLine(i); 
              
           20.  } 
              
           21. } 
              
              //output:
           22.        
              
           23. 6 
              
                     Extension Methods Chaining 
                     Like as instance methods, extension methods also have chaining capability. 
                          1.  public static class ExtensionClass 
                              
                          2. { 
                              
                          3. public static string Pluralize (this string s) {...} 
                              
                          4. public static string Capitalize (this string s) {...} 
                              
                          5. } 
                              
                          6. //we can do chainig of above methods like as 
                              
                          7. string x = "Products".Pluralize().Capitalize();  
                              
                     Ambiguity, resolution and precedence of extension method 
                     To use an extension method, it should be in same scope of class.If two extension methods have the same 
                     signature,the more specific method takes precedence. 
                          1.  static class StringExtension 
                              
                                  // first method
                          2. {                               
                              
                          3.  public static bool IsCapitalized (this string s) {...} 
                              
                          4. } 
                              
                          5. static class ObjectExtension 
                              
                          6. { 
                              
                                // second method
                          7.                                 
                              
                          8.  public static bool IsCapitalized (this object s) {...} 
                              
                          9. } 
                              
                                 // code here
                          10.                          
                                
                                 // first method is called
                          11.                                                 
                                
                          12. bool flag1 = "Dotnet-Tricks".IsCapitalized();  
                                
                                 // second method is called
                          13.                                                   
                                
                          14. bool test2 = (ObjectHelper.IsCapitalized ("Dotnet-Tricks"));  
                                
                     Key points about extension methods 
                1.   An extension method is defined as static method but it is called like as instance method. 
                2.   An extension method first parameter specifies the type of the extended object, and it is preceded by the 
                     "this" keyword. 
                3.   An extension method having the same name and signature like as an instance method will never be 
                     called since it has low priority than instance method. 
                4.   An extension method couldn't override the existing instance methods. 
                5.   An extension method cannot be used with fields, properties or events. 
                6.   The compiler doesn't cause an error if two extension methods with same name and signature are defined 
                     in  two  different  namespaces  and  these  namespaces  are  included  in  same  class  file  using  directives. 
                     Compiler will cause an error if you will try to call one of them extension method. 
                      
                     Source : http://www.dotnet-tricks.com/Tutorial/csharp/2HcK190612-C-Sharp-Extension-method.html 
The words contained in this file might help you see if this file matches what you are looking for:

...C sharp extension method an is a static of class that can be invoked using the instance syntax methods are used to add new behaviors existing type without altering in this keyword with first parameter and will extended by conceptually provides as implementation decorator structural pattern at compile time call translated into ordinary simple example defining public myextensions int wordcount string str return split char length program void main s dot net tricks calling i console writeline output chaining like also have capability extensionclass pluralize capitalize we do chainig above x products ambiguity resolution precedence use it should same scope if two signature more specific takes stringextension bool iscapitalized objectextension second object code here called flag dotnet test objecthelper key points about defined but specifies preceded having name never since has low priority than couldn t override cannot fields properties or events compiler doesn cause error different namespa...

no reviews yet
Please Login to review.