using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using nsStringExtensions;
enum methodType
{
legacy
, extensionMethod
};
class HelloWorld
{
static Char delimeter = ' ';
static String FORMAT_ITEM_WORDCOUNT = "{0}> {1} - [{2}]";
static void Main()
{
lyricBabyFaceSoonAsIGetHome();
}
static void lyricBabyFaceSoonAsIGetHome()
{
// Create and initializes a new StringCollection.
StringCollection objLyric = null;
List<int> objWordCountLegacy = null;
List<int> objWordCountExtensionMethod = null;
objLyric = new StringCollection();
objLyric.Add("I give good love");
objLyric.Add("I'll buy your clothes");
objLyric.Add("I'll cook your dinner too (yeah)");
objLyric.Add("Soon as I get home from work");
objWordCountLegacy = wordCount
(
objLyric
// method type
, methodType.legacy
, delimeter
);
Console.WriteLine("Legacy");
Console.WriteLine("------");
wordCountList
(
objLyric
, objWordCountLegacy
, delimeter
, FORMAT_ITEM_WORDCOUNT
);
objWordCountExtensionMethod =
wordCount
(
objLyric
// method type
, methodType.extensionMethod
, delimeter
);
Console.WriteLine("extensionMethod");
Console.WriteLine("---------------");
wordCountList
(
objLyric
, objWordCountExtensionMethod
, delimeter
, FORMAT_ITEM_WORDCOUNT
);
objLyric = null;
objWordCountLegacy = null;
objWordCountExtensionMethod = null;
}
static List<int> wordCount
(
StringCollection lyricList
, methodType methodTypeIs
, Char delimeter
)
{
List<int> objL = new List<int>();
int iNumberOfEntries =-1;
int iNumberofWords = -1;
int i = 0;
String strItem = null;
string[] strItemArray = null;
/*
If object is not valid, exit function
*/
if (lyricList==null)
{
return null;
}
//get number of nodes in collection class
iNumberOfEntries = lyricList.Count;
//Iterate List
for
(
i = 0;
i < iNumberOfEntries;
i++
)
{
//reset local variables
iNumberofWords = -1;
//Get Lyric Item
strItem = lyricList[i];
/*
Sanitize item before processing
*/
if (strItem != null)
{
if (methodTypeIs == methodType.legacy)
{
iNumberofWords = wordCount
(
strItem
, delimeter
);
}
else if (methodTypeIs == methodType.extensionMethod )
{
// call extension method
iNumberofWords = strItem.wordCount
(
delimeter
);
}
}
//Add Item to Array
objL.Add(iNumberofWords);
}
return ( objL );
}
static void wordCountList
(
StringCollection lyricList
, List<int> wordCountList
, Char delimeter
, String FORMAT_ITEM_WORDCOUNT
)
{
int iNumberOfEntries =-1;
int i = 0;
String strItem = null;
int iWordCount = 0;
String strLog = null;
//get number of nodes in collection class
iNumberOfEntries = lyricList.Count;
//Iterate List
for
(
i = 0;
i < iNumberOfEntries;
i++
)
{
//Get Lyric Item
strItem = lyricList[i];
//Get Word Count
iWordCount = wordCountList[i];
strLog = String.Format
(
FORMAT_ITEM_WORDCOUNT
, (i+1)
, strItem
, iWordCount
);
Console.WriteLine(strLog);
}
}
static int wordCount
(
String item
, Char delimeter
)
{
int iWordCount = -1;
string[] strItemArray = null;
//split item
if (
( item != null)
&& ( delimeter != null)
)
{
// using delimeter split word
strItemArray = item.Split
(
delimeter
);
//get number of words
iWordCount = strItemArray.Length;
}
return (iWordCount);
} // method wordCount
}
namespace nsStringExtensions
{
public static class stringExtensions
{
public static int wordCount
(
this string strItem
, char delimeter = ' '
)
{
int iNumberofWords =-1;
string[] strItemArray = null;
if (
( strItem != null)
&& ( delimeter != null )
)
{
//split item
strItemArray = strItem.Split(delimeter);
//get number of words
iNumberofWords = strItemArray.Length;
}
return (iNumberofWords);
} // method - wordCount
} //class stringExtensions
} //namespace - nsStringExtensions