Static Classes and Static Class Members

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
In other words, you cannot use the new keyword to create a variable of the class type.
Because there is no instance variable, you access the members of a static class by using the class name itself.

For example, if you have a static class that is named UtilityClass that has a public method named MethodA, you call the method as shown in the following example:
UtilityClass.MethodA();

A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields.
For example, in the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class.
That is, you apply the members of the class by specifying the class name and the method name, as shown in the following example.
double dub = -3.14;
Console.WriteLine(Math.Abs(dub));
Console.WriteLine(Math.Floor(dub));
Console.WriteLine(Math.Round(Math.Abs(dub)));

// Output:
// 3.14
// -4
// 3

As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded.
The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program.
A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.

To create a non-static class that allows only one instance of itself to be created, see Implementing Singleton in C#.

The following list provides the main features of a static class:
Contains only static members.
Cannot be instantiated.
Is sealed.
Cannot contain Instance Constructors.

Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor.
A private constructor prevents the class from being instantiated.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added.
The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.
Static classes cannot contain an instance constructor; however, they can contain a static constructor.
Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.
For more information, see Static Constructors (C# Programming Guide).

Static Classes and Static Class Members的更多相关文章

  1. Java中主类中定义方法加static和不加static的区别

     Java中主类中定义方法加static和不加static的区别(前者可以省略类名直接在主方法调用(类名.方法),后者必须先实例化后用实例调用) 知识点:1.Getter and Setter 的应用 ...

  2. Java的初始化执行顺序(父类static变量->子类static变量->父类成员变量->父类构造器->成员变量->构造器->main函数)

    1. 引言 了解Java初始化的顺序,有助于理解Java的初始化机制和内存机制. 顺序:父类static变量->子类static变量->父类成员变量->父类构造器->成员变量- ...

  3. 对php和java里面的static函数和static的一些理解

    static function: "static方法就是没有this的方法.在static方法里面不可以调用非静态方法,反过来是可以的.并且可以在没有创建任何对象的前提下,仅仅通过类名来调用 ...

  4. static, const 和 static const 变量的初始化问题

    const 常量的在超出其作用域的时候会被释放,但是 static 静态变量在其作用域之外并没有释放,只是不能访问. static 修饰的是静态变量,静态函数.对于类来说,静态成员和静态函数是属于整个 ...

  5. Java中的内存处理机制和final、static、final static总结

    Java中的内存处理机制和final.static.final static总结   装载自:http://blog.csdn.net/wqthaha/article/details/20923579 ...

  6. 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类

    不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中st ...

  7. C++中类中常规变量、const、static、static const(const static)成员变量的声明和初始化

    C++类有几种类型的数据成员:普通类型.常量(const).静态(static).静态常量(static const).这里分别探讨以下他们在C++11之前和之后的初始化方式. c++11之前版本的初 ...

  8. 094 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 02 static关键字 04 static关键字(续)

    094 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...

  9. 093 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 02 static关键字 03 static关键字(下)

    093 01 Android 零基础入门 02 Java面向对象 02 Java封装 01 封装的实现 03 # 088 01 Android 零基础入门 02 Java面向对象 02 Java封装 ...

随机推荐

  1. FolderBrowserDialog使用

    private void button_browse_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderB ...

  2. Unity5.0 手动激活

    提供Unity5.0.1.f1(32-bit)下载http://pan.baidu.com/s/1bg5sDK 密码 ns75 有时候会发现,用激活工具是激活不了的,这个时候就要手动激活,其实个人觉得 ...

  3. U3D 摄像机镜头控制

    如果要实现,摄像机跟随着主角运动,还有运用滚轮实现镜头的方法和缩小的实现原理 方法1:把主摄像机放到主角的下面,作为一个子对象,调整好摄像机的视角,此时就会跟随了. 方法2:用代码让摄像机的相关的po ...

  4. TCP的服务流程

    TCP协议提供的是可靠的.面向连接的传输控制协议,即在传输数据前要先建立逻辑连接,然后再传输数据,最后释放连接3个过程.TCP提供端到端.全双工通信:采用字节流方式,如果字节流太长,将其分段:提供紧急 ...

  5. 《你不常用的c#之三》:Action 之怪状

    转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6199891 例1:   public static void Main() { Li ...

  6. hadoop_并行写操作思路_2

    如果想实现将 Client端的 File并行写入到 各个Datanode中, 首先, 应该修改的是,DistributedFileSystem中的create方法, 在create 内部调用FSNam ...

  7. javascript:console.log()是什么js库里的?

    这个不是什么库的,这个是浏览器的函数,如果你使用firefox并且装有firebug插件,当使用console.log(……)时,会把括号内的字符串输出到控制台,当然,在IE中这个是没有的,要报错.相 ...

  8. 对有状态bean和无状态bean的理解(转)

    现实中,很多朋友对两种session bean存在误解,认为有状态是实例一直存在,保存每次调用后的状态,并对下一次调用起作用,而认为无状态是每次调用实例化一次,不保留用户信息.仔细分析并用实践检验后, ...

  9. Set集合中的HashSet集合

    HashSet集合的特点:元素是具备唯一性的,每次存储都要先算出哈希值,看有没相同,没有相同的存储到相应的位置,如果相同则再判断存储进来的值是否与被比较的相同,如果相同,则不再存储,不同就存储 pac ...

  10. C++自定义异常处理

    自定义异常类 class MyException { public: MyException() { } MyException(char* str) { msg = str; } MyExcepti ...