There are times when you don't need an entire class to perform some action. If you need a method that, for example, redirects a user to a specific URL, instantiating an entire class would be overkill. That's where static methods come in!
Marking a method static (or Shared in VB) means that you don't have to create an instance of the containing class in order to use it. A redirect method that you might use across your entire application might look like the method in Listing 2.14.
C#
public class MyMethods { public static void SpecialRedirect() { Response.Redirect("somepage.aspx"); } }
VB.NET
Public Class MyMethods Public Shared Sub SpecialRedirect() Response.Redirect("somepage.aspx") End Sub End Class
Calling this method is just a matter of specifying the class name itself, followed by the method name:
MyMethods.SpecialRedirect()
I find that in almost every large project, there are little pieces of functionality that don't really fit anywhere and that certainly don't merit their own class. Inevitably, I end up with a class called Utility with several static methods like the previous one.