static, readonly, const
- 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.
- 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. - 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).
- Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier
- 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;
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的更多相关文章
- 读书笔记:const和readonly、static readonly 那些事
C#中表示不变的量(常量)的两种形式:const 和readonly const 是静态常量 readonly 是动态常量 严格的来讲:const 应该称为常量 而readonly 则应称为只读变量. ...
- 到底是 const 还是 static readonly
真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试验. 程序集内的常量 现在我们建立一个程序,里面有一个MyCl ...
- 【转】const和static readonly
我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声 ...
- (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 ...
- const 还是 static readonly
到底是 const 还是 static readonly 真的一样? const 和 static readonly 常在程序中用来声明常量,调用方法也没有什么不同,他们真的一样吗?我们可以做个试 ...
- Unity C# const与static readonly的区别与联系
using System; namespace Test { class MainClass { //懒人写法的单例 class Weapon { public static readonly Wea ...
- const和static readonly的区别
我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等. 在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在 ...
- const和static readonly 区别
const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly ...
- C# const和static readonly区别
[转]C# const和static readonly区别 以前只是知道Const和static readonlyd的区别在于const的值是在编译期间确定的,而static readonly是在运行 ...
随机推荐
- 在silverlight中通过WCF连接ORACLE DB数据库(转)
转自 http://hi.baidu.com/qianlihanse/item/458aa7c8d93d4e0cac092ff4 这不是我的原创,我也是上网学习的~ How to get data f ...
- javascript 自调用函数 闭包
<script type="text/javascript"> var car = {name:"lhs",model:500}; window.o ...
- swift 常见报错及解决方案
当你初学swift,或者你从2.0转到3.0,见点红是在所难免再寻常不过的事情,其实一般也都是一些小的语法问题,度娘一般都有能力告诉你答案,但是凡事做个总结做到心中有数毕竟是比度娘靠谱的. 1.can ...
- NSString和NSMutableString的创建及其一些常用方法
NSString和NSMutableString都是对象类型,是NSObject的子类.NSString是不可变字符串,NSMutableString是可变字符串 一.NSString的创建 1.创建 ...
- 利用Resgen.exe 批量生成resources文件
Resgen.exe(资源文件生成器) 您可以直接如图操作 转换时在 文本中先写好要转换的文件然后 全选 复制到控制台中 Filename.resx 要转换的文件 ResName1.resource ...
- XproerIM V1,2,12,65376 发布。
客户端下载:http://yunpan.cn/QTCxKvcpC4Iet 访问密码 9141 更新说明:1.增加表情功能. 更新代码截图:
- 性能改善之For与Foreach
关于For与Foreach的区别,博客园里已经有好多这样文章了,都分析的挺好:http://www.cnblogs.com/jobs/archive/2004/07/17/25218.aspx 不过 ...
- MFC 配合 protobuff libevent 实现的Socket 的GM工具 框架
MFC 配合 protobuff libevent 实现的Socket 的GM工具 框架
- (十二) 一起学 Unix 环境高级编程 (APUE) 之 进程间通信(IPC)
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- Prim算法和Kruskal算法(图论中的最小生成树算法)
最小生成树在一个图中可以有多个,但是如果一个图中边的权值互不相同的话,那么最小生成树只可能存在一个,用反证法很容易就证明出来了. 当然最小生成树也是一个图中包含所有节点的权值和最低的子图. 在一个图中 ...