c# Example found on the net, demonstrated how Dot Net 3 extensions are declared and consumed.
namespace myExtensionExample
{
class Program
{
static void Main(string[] args)
{
int[] values = { 5, 6, 10 };
bool isInArray = 7.In(values);
string[] s_vals = { "5", "6", "10" };
bool isInStringArray = "10".In(s_vals);
string s = "Hello Extension Methods";
int i = s.WordCount();
}
}
//define extension methods within a static class
public static class myExtensions
{
//'this' parameter is usually used on object where the method is applied to
public static int WordCount(this string str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
public static bool In(this T o, IEnumerable items)
{
foreach (T item in items)
{
if (item.Equals(o))
return true;
}
return false;
}
}
}
Original materials from http://msdn.microsoft.com/en-us/library/bb383977.aspx
