Static classes are usually used as ‘utility’ classes
You don’t need an instance
class Program
{
static void Main()
{
double result = thing.daveSubtract(10);
Console.WriteLine(result);
}
}
static class thing
{
static public double daveSubtract(double number)
{
return (number - 2);
}
}
Extension Methods
From: http://msdn.microsoft.com/en-us/library/bb383977.aspx
Extension methods enable you to "add" methods to existing types without creating a new derived type,
Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.
For client code, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "hello extension methods are good";
Console.WriteLine(s);
int i = s.WordCount();
Console.WriteLine("number of words is {0}", i.ToString());
}
}
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}