1.Constants

is a symbol that has a never-changing value.  its value must be determinable at compile time.

使用范围:

1.The compiler then saves the constant’s value in the assembly’s metadata.This means that you can define a constant only for types that your compiler considers primitive types.

In C#, the following types are primitives and can be used to define constants: Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, and String.

2.However, C# also allows you to define a constant variable of a non-primitive type if you set the value to null.

作用:

1.Because a constant value never changes, constants are always considered to be part of the defining type.

In other words, constants are always considered to be static members, not instance members.

2.Defining a constant causes the creation of metadata.

编译原理:

When code refers to a constant symbol, compilers look up the symbol in the metadata of the assembly that defines the constant, extract the constant’s value, and embed the value in the emitted Intermediate Language (IL) code.

特征:

1.Because a constant’s value is embedded directly in code, constants don’t require any memory to be allocated for them at run time.

2.In addition, you can’t get the address of a constant and you can’t pass a constant by reference.

can’t use constants if you need to have a value in one assembly picked up by another assembly at run time (instead of compile time). Instead, you can use readonly fields.

3.These constraints also mean that constants don’t have a good cross-assembly versioning story.

if you changes the constant value,only rebuilds the DLL assembly is not affected,it will have to be recompiled as well.

使用环境:

so you should use them only when you know that the value of a symbol will never change

示例说明1:

  

  

  MaxEntriesInList is a constant literal with a value of 50 and embeds the Int32 value of 50 right inside the application’s IL code

  In fact, after building the application assembly, the DLL assembly isn’t even loaded at run time and can be deleted from the disk

  because the compiler does not even add a reference to the DLL assembly in the application's metadata.

2.Fields

A field is a data member that holds an instance of a value type or a reference to a reference type.

修饰符:

  

分类:

  the common language run time (CLR) supports both type (static) and instance (nonstatic) fields.

  1.For type fields, the dynamic memory required to hold the field’s data is allocated inside the type object, which typically happens the first time any method that references the type is just-in-time (JIT)–compiled

  2.For instance fields, the dynamic memory to hold the field is allocated when an instance of the type is constructed.

特征:

  1.Because fields are stored in dynamic memory, their value can be obtained at run time only.

  Fields solve the versioning problem that exists with constants.

  2.a field can be of any data type, so you don’t have to restrict yourself to your compiler’s built-in primitive types.

  3.The CLR supports readonly fields and read/write fields.

  Most fields are read/write fields,meaning the field’s value might change multiple times as the code executes.

  However, readonly fields can be written to only within a constructor method (which is called only once, when an object is first created). Compilers and verification ensure that readonly fields are not written to by any

method other than a constructor.

  but, reflection can be used to modify a readonly field.

注意:

  When a field is of a reference type and the field is marked as readonly, it is the reference that is immutable, not the object that the field refers to.

   

示例1:fix the versioning problem by using a static readonly field

  

  rebuild it.when the application’s Main method runs,the CLR will load the DLL assembly (so this assembly is now required at run time) and grab the value of the MaxEntriesInList field out of the dynamic memory allocated for it.

  changes the 50 to 1000 and rebuilds the assembly,pick up the new value: 1000.A caveat: this scenario assumes that the new version of the DLL assembly is not strongly named and the versioning policy of the application is such that the CLR loads this new version.

示例2:how to define a readonly static field that is associated with the type itself, as well as read/write static fields and readonly and read/write instance fields

  

7.Constants and Fields的更多相关文章

  1. CLR via C# 3rd - 07 - Constants and Fields

    1. Constants        A constant is a symbol that has a never-changing value. When defining a constant ...

  2. 探秘Tomcat——连接器和容器的优雅启动

    前言: 上篇<探秘Tomcat——启动篇>粗线条的介绍了在tomcat在启动过程中如何初始化Bootstrap类,加载并执行server,从而启动整个tomcat服务,一直到我们看到控制台 ...

  3. Delphi XE5教程11:Tokens

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...

  4. Android开发基础规范(二)

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52614696 前言:Androi ...

  5. Tomcat自定义classLoader加密解密

    class很好反编译,所以需要对class文件先进行加密,然后使用自己的classloader进行解密并加载. [步骤] 大概分两步: 1.对class文件进行加密 2.写解密class文件并加载的c ...

  6. 我遇到的WPF的坑

    转自 林德熙Blog 本文:我遇到的WPF的坑 目录 单例应用在多实例用户无法使用 标记方法被使用 当鼠标滑过一个被禁用的元素时,让ToolTip 显示 获取设备屏幕数量 获取当前域用户 绑定资源文件 ...

  7. sonar:默认的扫描规则

    https://blog.csdn.net/liumiaocn/article/details/83550309 https://note.youdao.com/ynoteshare1/index.h ...

  8. 2019-8-28-WPF-开发

    title author date CreateTime categories WPF 开发 lindexi 2019-8-28 11:3:39 +0800 2018-2-13 17:23:3 +08 ...

  9. 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

    访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...

随机推荐

  1. java基础整理1

    1.匿名对象:new persion().tell();这样的,它只开辟栈内存,没有栈引用的关系 2.构造方法的名称必须与类名称一致,构造方法的声明处不能有任何返回值类型的声明,不能在构造方法中使用r ...

  2. Mongodb 笔记08 了解应用的动态、数据管理、持久性

    了解应用的动态 1. 了解正在进行的操作:db.currentOp() , 可以加过滤条件,从而只显示符合条件的结果. 1). 寻找有问题的操作:db.currentOp() 最常见的操作就是用来寻找 ...

  3. 使用 Delphi Xe 的 TDictionary

    原本一直使用 TList, 将定义的一个个 Record 保存在TList 里面, 为了能把某些对象管理起来, 例如一个类的 n 多实例,可以进行索引.查找.释放等 今天刚看到原来已经有了一个叫 TD ...

  4. STM32外部中断.

    void EXTIX_Init(void){     EXTI_InitTypeDef EXTI_InitStructure;    NVIC_InitTypeDef NVIC_InitStructu ...

  5. C# csv 操作类

    using System.Data; using System.IO; using System.Text; namespace YanZhiwei.DotNet2.Utilities.Common ...

  6. php curl应该怎么使用呢

    原php默认并不进行此项功能的扩展,但还是有的,只是没有让它生效罢了.打开PHP安装目录,搜索以下三个文件 ssleay32.dll.libeay32.dll和 php_ curl .dll,一一拷贝 ...

  7. 本人整理的一些PHP常用函数

    <?php //===============================时间日期=============================== //y返回年最后两位,Y年四位数,m月份数字 ...

  8. 函数参数为int*和int&的区别

    参数为int*,表明参数为指针,调用的时候需要地址,如f(&a): 参数为int&,传引用参数,调用时f(a),“引用类型的形参就通过形实结合,成为实参的一个别名,对形参的任何操作也就 ...

  9. js命名空间笔记

    在量比较大或者多人编写的情况下,命名冲突就很有可能发生,同一个页面引用了两个命名相同功能不同的文件,调用的时候就会出问题.因此使用JS命名空间很重要. 1.采用字面量方法创建命名空间: var a={ ...

  10. Java与.NET 的Web Services相互调用

    一:简介 本文介绍了Java与.NET开发的Web Services相互调用的技术.本文包括两个部分,第一部分介绍了如何用.NET做客户端调用Java写的Web Services,第二部分介绍了如何用 ...