开发者

What is different between a static class that has static method, and a a regular class that has a static method?

开发者 https://www.devze.com 2023-04-10 02:47 出处:网络
Example: public class person { public String Name {get;set;} } public static class FactoryStatic 开发者_Go百科{

Example:

public class person
{
    public String Name {get;set;}
}

public static class FactoryStatic
开发者_Go百科{
    public static person Create(string name)
    {
        return new person() {Name =name};
    }
}

public class FactoryNoneStatic
{
    public static person Create(string name)
    {
        return new person() {Name =name};
    }
}

My question is what is difference between those two factory class, and when to use them?


For the direct use, the creation of a Person object, there is no difference.

It is however possible to create an instance of FactoryNoneStatic, which is probably not desirable.

By marking the class as static you are clear about your intent and you prevent mis-use of the class.

So in this situation, use the static class FactoryStatic.


A static class can only contain static members. So you cannot create an instance of a static class.


A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

The main features of a static class are:

They only contain static members.

They cannot be instantiated.

They are sealed.

They cannot contain Instance Constructors.

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

I might be stating this wrong, but if you have a static class, it's usually because everything in that class can be static, and there's never a reason to maintain an instance of it.

If you have static members of a non-static class, you still want to maintain state of that object (or at least carry an instance of it), but some methods that relate to it can be called without instantiating the class.


In general not much. Marking the class as static makes it immediately obvious that it can only have static members. If a class isn't marked as static then the assumption is that there are non-static members.

There's one situation that I'm aware of where you must use a static class is that extension methods are only allowed in static classes

As for when to use them, if the class only contains static members, then mark the class as static to make it obvious. There may be some extremely minor performance differences, but I'd consider them negligible.


A static class can have only static members in it. Also, it is not possible to create instances of a static class using the new keyword.

On the other hand a static methodis callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member.


Simply a static class can not be instansiated ex:

public static class StaticClass
{
    public static void Function()
    { /* */ }
}

You can't do like this:

StaticClass sc = new StaticClass();

You will do it like this:

StaticClass.Function();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号