.Net:- Extension Methods in C#

Background

Object Oriented Programming Languages allow various features to add additionally functionality to existing classes.

 

Tooling

Here are some of the available faculties:-

  1. Inheritance
  2. Interfaces

 

Base Class

String Class

Methods

Depending on the programming object certain methods may be exposed.

Methods such as:-

  1. Length
  2. Upper
  3. Lower
  4. Split
  5. Word Count
  6. Proper Case
  7. Reverse
  8. Convert
    • Convert to another datatype
  9. Contains
    • Contains Unicode Characters

 

Method – Word Count

Outline

A trivial need is to count the number of words in a sentence.

One can tokenize the sentence based on whitespaces and count the number of tokens.

Method – wordCount


 static int wordCount
             (
                   String item
                 , Char   delimiter
             )
  {
      
    int iWordCount = -1;  
    string[] strItemArray = null;
    
     //split item
     if (
              ( item != null)
           && ( delimiter != null)
        )   
     {
         
        // using delimiter split word 
        strItemArray = item.Split
                        (
                            delimiter
                        ); 
                
        //get number of words
        iWordCount = strItemArray.Length;
        
     }
    
    return (iWordCount);
      
  } // method wordCount
  

 

Usage – Method – wordCount

To use our method, we will pass the string and our delimiter.


 String strItem;
 char   chDelimiter;
 int    iNumberofWords = -1;

 strItem = "Jack and Jill are friends of ours";
 chDelimeter = ' ';
 
 iNumberofWords = wordCount 
                   ( 
                        strItem
                      , chDelimiter 
                   );          

 

Extension Method – Word Count

Biases

Enough jerking for code on StackOverflow and one will see posters recommend refactoring the code as an Extension Method.

Requirements

Here are the requirements for extension methods:-

  1. class
    • static class
  2. method
    • static method
  3. Method Arguments
    • first argument
      • this keyword
      • object type
      • object name

Extension Method – Our definition

  1. class
    • static class
      • sample
        • public static class stringExtensions
  2. method
    • static method
      • sample
        • public static int wordCount 
  3. Method Arguments
    • first argument definition
      • this keyword
      • object type
      • object name
    • first argument sample
      • this string strItem

 

Extension Method – Code



namespace nsStringExtensions
{
    
    public static class stringExtensions
    {
        public static int wordCount
        (
              this string strItem
            , char delimiter = ' '
        )
        {  
             
            int iNumberofWords =-1;
            string[] strItemArray = null;
            
            if (
                       ( strItem != null)
                    && ( delimiter != null )
               )
            {
                
                 //split item
                 strItemArray = strItem.Split(delimiter); 
                
                 //get number of words
                 iNumberofWords = strItemArray.Length;
                 
            }
            
            return (iNumberofWords);
        
        } // method - wordCount
        
    } //class stringExtensions

} //namespace - nsStringExtensions

 

Usage – ExtensionMethod – wordCount

To use our method, we will directly invoke the method against our object.


 String strItem;
 char   chDelimiter;
 int    iNumberofWords = -1;

 strItem = "Jack and Jill are friends of ours";
 chDelimeter = ' ';
 
 iNumberofWords = strItem.wordCount 
                   ( 
                       chDelimiter 
                   );          

 

Source Code Control

  1. GitLab
    • String – Number of Words Using Extension Method
      Link
  2. GitHub
    • DanielAdeniji/stringNumberofWordsUsingExtensionMethod.cs
      Link

Online Code

  1. OnlineGDB
    • wordCountLyricInCSharp
      Link

 

References

  1. Wikipedia
    • Extension Method
      Link
  2. Dart
    • Extension Methods
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s