Static Classes and Methods Practical Examples [duplicate]
Possible Duplicate:
When to Use Static Classes in C#
I understand the concept of a static classes and static class members but I'm sure I could be using them a lot more on my projects. Could someone show me some practical examples of when they have created and used static classes in C#?
Tha开发者_JAVA百科nks
The most practicle use of Static classes and Methods are to create extension methods. Like the Linq Query extension methods.
Any time you need to represent a process in your design, as opposed to an entity, or a value, you should use a static class. A Process is an object, or a thing, within a design, that has no state, but manipulates or manages the state of other objects. It will have static methods that touch no private or public state variables of the class itself, but only operate on the public properties of the objects passed to these methods.
Some examples might include a class that performs the scheduling function for an academic institition,
or a class that is responsible for processing frieght and shipping containers at a port of entry for loading onto ships or aircraft.
Utility classes, Helper classes, conversion classes... any group of methods that do not make sense to place on an instance, but instead work with other class instances are good candidates to be made static. There are plenty of other examples - these are just a few.
Most times I use static
classes is for Tools, utilities functions.
A static class (using the static keyword) is a class that is both Abstract and Sealed. Therefore static members are allowed. You need to use a static class to implement extension methods as this is arbitrated by the c# compiler.
Consider the singleton pattern - you want to use one instance and one instance only:
public class MySingleton
{
protected static Lazy<MySingleton> _instance = new Lazy<MySingleton>(() => new MySingleton());
protected MySingleton() {}
public static MySingleton Instance
{
get { return _instance.Value; }
}
}
The way the class is constructed, you can only ever use the one instance, stored in the static variable. Consider the HttpContext.Current
static property.
In addition, static items are useful for state maintenance; say you want to cache some data in memory that any instance needs, but there is no need to refetch the data.
You also are required to make static classes for extension methods.
In response to your request for an example of an extension method, this will capitalize the first letter of a string.
public static class StringExtensions
{
public static string UppercaseFirst(this String s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
}
Somewhere else in your code, you could use it like this
var pirate = "yar";
System.Diagnostics.Debug.WriteLine(pirate.UppercaseFirst());
//Yar
Static Classes are like a container for holding methods and other data.
Consider a Math Utilities class. You wouldn't want to creat an instance of MathUtility, but you might define a constant (such as PI
) or some methods for math functionality (static double sqrt(double)
).
You could do all of these things in a non-static class; but they might not have much to do with the class they are attached to.
They are also REQUIRED for extension methods - which allow you to enhance the functionality of pre-defined classes.
See link for the Math class in C#.
精彩评论