What is a “static” class?

后端 未结 10 1364
面向向阳花
面向向阳花 2020-12-04 16:35

In C# what is the difference between:

public static class ClassName {}

And:

public class ClassName {}
相关标签:
10条回答
  • 2020-12-04 17:21

    Static variable in c

    a variable local to a class as auto variables but static variable do not disappear as function is no longer active.Their values persist.If control comes back,static variables have same value

    static function in c functions that are not visible to functions in other files.

    *static data members in cpp * data members can be variables or functions in cpp static applies to both data members the class itself can be static "There is only one copy of static data memberss shared by all objects in that class" static data members can access only static data members

    static class this class cannot instantiate objects

    0 讨论(0)
  • 2020-12-04 17:22

    Static classes and members are used to create data and methods that can be accessed without creating an instance (using the new keyword, they cannot have a constructor) of the class.

    Static classes can be declared when there is no dependence on the its own object identity, so a static class must contain only static members.

    This classes are loaded by the CLR when the program or namespace containing the class is loaded.

    They are also sealed, cannot be inherited from.

    0 讨论(0)
  • 2020-12-04 17:27

    http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html - very good article on this. This is for Java. But i think concept should should same in C# too.

    0 讨论(0)
  • 2020-12-04 17:30

    Firstly, a comment on an answer asked about what "static" means. In C# terms, "static" means "relating to the type itself, rather than an instance of the type." You access a static member (from another type) using the type name instead of a reference or a value. For example:

    // Static method, so called using type name
    Guid someGuid = Guid.NewGuid();
    // Instance method, called on a value
    string asString = someGuid.ToString();
    

    Now, static classes...

    Static classes are usually used as "utility" classes. The canonical example is probably System.Math. It doesn't make sense to create an instance of math - it just "is". A few rules (both "can" and "can't"):

    • Static classes always derive from object. You can't specify a different base type, or make the static class implement an interface.
    • Static classes can't have any instance members - all variables, methods etc must be static.
    • Static classes can't declare any instance constructors and the compiler doesn't create a parameterless constructor by default. (Before static classes came in C# 2.0, people would often create an abstract class with a private constructor, which prevented instantiation. No need here.)
    • Static classes are implicitly abstract (i.e. they're compiled to IL which describes an abstract class) but you can't add the abstract modifier yourself.
    • Static classes are implicitly sealed (i.e. they're compiled to IL which describes an sealed class) but you can't add the sealed modifier yourself.
    • Static classes may be generic.
    • Static classes may be nested, in either non-static or static classes.
    • Static classes may have nested types, either non-static or static.
    • Only static, top-level non-generic classes can contain extension methods (C# 3.0).
    0 讨论(0)
提交回复
热议问题