Background
Object Oriented Programming Languages allow various features to add additionally functionality to existing classes.
Tooling
Here are some of the available faculties:-
- Inheritance
- Interfaces
Base Class
String Class
Methods
Depending on the programming object certain methods may be exposed.
Methods such as:-
- Length
- Upper
- Lower
- Split
- Word Count
- Proper Case
- Reverse
- Convert
- Convert to another datatype
- 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:-
- class
- static class
- method
- static method
- Method Arguments
- first argument
- this keyword
- object type
- object name
- first argument
Extension Method – Our definition
- class
- static class
- sample
- public static class stringExtensions
- sample
- static class
- method
- static method
- sample
- public static int wordCount
- sample
- static method
- Method Arguments
- first argument definition
- this keyword
- object type
- object name
- first argument sample
- this string strItem
- first argument definition
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
- GitLab
- String – Number of Words Using Extension Method
Link
- String – Number of Words Using Extension Method
- GitHub
- DanielAdeniji/stringNumberofWordsUsingExtensionMethod.cs
Link
- DanielAdeniji/stringNumberofWordsUsingExtensionMethod.cs
Online Code
- OnlineGDB
- wordCountLyricInCSharp
Link
- wordCountLyricInCSharp