• static

    • Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier
      can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
    • A constant or type declaration is implicitly a static member.
    • A static member cannot be referenced through an instance. Instead, it is referenced through the type name.
    • While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
    • It is not possible to use this to reference static methods or property accessors.
    • If the static keyword is applied to a class, all the members of the class must be static.
    • Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated.
    • 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.
    • 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.
    • 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.
    • 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.
    • A non-static class can contain static methods, fields, properties, or events.
    • The static member is always accessed by the class name, not the instance name. Static methods and properties cannot access non-static
      fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
    • It is more typical to declare a non-static class with some static members, than to declare an entire class as static.
    • Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
    • Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
    • Although a field cannot be declared as static const, a const field
      is essentially static in its behavior. It belongs to the type, not to instances of the type.
    • Static members are initialized before the static member is accessed for the first time and before the static constructor, if there
      is one, is called.
    • C# does not support static local variables (variables that are declared in method scope).

  • const
    • The static modifier is not allowed in a constant declaration.
    • A const field can only be initialized at the declaration of the field. A readonly field can be initialized
      either at the declaration or in a constructor. Therefore,readonly fields can have different values depending on the constructor used. Also,
      although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;
Examples:
   public static class MyStaticClass : MyClass // error CS0713: Static classes must derive from object.
{
static MyStaticClass()
{
Console.WriteLine("MyStaticClass constructor");
}
public static void Test() { Console.WriteLine("MyStaticClass:Test"); }
} public class Derived : MyStaticClass { } //'Derived': cannot derive from static class 'MyStaticClass'

static, readonly, const的更多相关文章

  1. 读书笔记:const和readonly、static readonly 那些事

    C#中表示不变的量(常量)的两种形式:const 和readonly const 是静态常量 readonly 是动态常量 严格的来讲:const 应该称为常量 而readonly 则应称为只读变量. ...

  2. 到底是 const 还是 static readonly

    真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试验. 程序集内的常量 现在我们建立一个程序,里面有一个MyCl ...

  3. 【转】const和static readonly

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...

  4. (C#) What is the difference between "const" and "static readonly" ?

    const int a must be initialized initialization must be at compile time readonly int a can use defaul ...

  5. const 还是 static readonly

    到底是 const 还是 static readonly   真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试 ...

  6. Unity C# const与static readonly的区别与联系

    using System; namespace Test { class MainClass { //懒人写法的单例 class Weapon { public static readonly Wea ...

  7. const和static readonly的区别

    我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等. 在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在 ...

  8. const和static readonly 区别

    const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly ...

  9. C# const和static readonly区别

    [转]C# const和static readonly区别 以前只是知道Const和static readonlyd的区别在于const的值是在编译期间确定的,而static readonly是在运行 ...

随机推荐

  1. VS调试技巧

    下面有从浅入深的6个问题,您可以尝试回答一下 一个如下的语句for (int i = 0; i < 10; i++){if (i == 5)j = 5;},什么都写在一行,你怎么在j=5前面插入 ...

  2. APPLICATION ERROR #1502 .

    mantisbt出现1502问题解决:引起问题的原因:当提交的问题有分类,此时删除此分类,就会出现下面的情况.问题描述:APPLICATION ERROR #1502 没有找到类别.请使用浏览器的“返 ...

  3. MySQL 批量插入 Update时Replace

    建一张试验表如下: 一.批量插入 MySQL的INSERT有一种写法如下: INSERT INTO person VALUES (NULL,'关羽', '2016-04-22 10:00:00'), ...

  4. jQuery 中 data 方法的实现原理

    前言:jQuery 作为前端使用最多最广泛的 JS 库,其源码每个 JSer 都应该研究一下.早就打算看却一直被各种事拖着,上次某公司面试时被问到 jQuery 中 data 方法是如何实现的,结果答 ...

  5. java应用死循环排查方法或查找程序消耗资源的线程方法(面试)

    今天遇到一个面试,怎么在一堆线程中查找一个死循环? 如果遇到线上应用cpu飙升,并出现OutOfMemery怎么办? 首先线上应用的jvm配置要养成良好的习惯,增加一下配置则可以在jvm发生 oom的 ...

  6. 25、继续echarts实现中国地图

    1. 以上是实现的效果 下边是实现的代码,上一篇地图没有颜色,是因为没有引入zrender包,因为echarts画地图是基于zrender实现的 <!DOCTYPE html> <h ...

  7. 解决ubuntu中apache2的url大小写敏感问题。

    cd /etc/apache2/mods-enabled ln -s ../mods-available/speling.load speling.load ln -s ../mods-availab ...

  8. linux网络完全与防护

    7.1 网络封包联机进入主机的流程   7.1.1 封包进入主机的流程 1.经过防火墙的分析 iptables 主要功能是封包过滤 主要分析TCP/IP的封包表头来进行过滤的机制 分析的是OSI的第二 ...

  9. JSF2 下 taglib 的问题

    在jsf1使用 taglib 定义 标签出现 The absolute uri: http://java.sun.com/jsf/core cannot be resolved in either w ...

  10. Python基础(七)内置函数

    今天来介绍一下Python解释器包含的一系列的内置函数,下面表格按字母顺序列出了内置函数: 下面就一一介绍一下内置函数的用法: 1.abs() 返回一个数值的绝对值,可以是整数或浮点数等. 1 2 3 ...