Instantiated Classes Versus Static Methods
Up to this point, you've seen data go in and out of a class instance via properties and methods. In Chapter 2, we looked briefly at static (shared) methods. These are class methods that do not require an instance of the class to be called. The .NET Framework is filled with methods like this. For example, you can call Convert.ToInt32(myString) to convert the value of a string to an integer without having to create an instance of the Convert class. Listing 2.13 from that chapter shows how you can declare static methods within a class.The decision to use a static method over using a class instance really comes down to your data requirements. If you have a dozen pieces of data and need to get three or four different calculated values based on that data, a single static method is obviously not going to suit your needs. Having several static methods wouldn't be any good either because you would need to feed all that data in for every method. It's much easier to create an instance of a class, assign values to its properties, and then call its methods.Starting with v2.0 of C# (Visual Studio 2005), you can declare an entire class as static. All of the methods in the class must also be declared static. Static classes have no constructors, so consuming code can't create an instance of the class.