1. Constants

 
     A constant is a symbol that has a never-changing value. When defining a constant symbol, its value must be determinable at compile time. 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. However, C# also allows you to define a constant variable of a non-primitive type if you set the value to null
 
using System;
public sealed class SomeType {
     // SomeType is not a primitive type but C# does allow
     // a constant variable of this type to be set to 'null'.
     public const SomeType Empty = null;
}
 
  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. Because a constant’s value is embedded directly in code, constants don’t require any memory to be allocated for them at runtime. 
 
  If the developer changes the constant and only rebuilds the DLL assembly, the application assembly which refers the constant is not affected. For the application to pick up the new value, it will have to be recompiled as well.
     
2. Fields
 
  A field is a data member that holds an instance of a value type or a reference to a reference type.
 
  Filed modifiers
     CLR Term          C# Term
     Static                static               The field is part of the type’s state, as opposed to being part of an object’s state.
     Instance            (default)           The field is associated with an instance of the type, not the type itself.
     InitOnly             readonly         The field can be written to only by code contained in a constructor method.
     Volatile             volatile            Code that accessed the field is not subject to some thread-unsafe optimizations that may be performed
                                                    by the compiler, the CLR, or by hardware. Only the following types can be marked volatile: all reference
                                                    types, Single, Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Char, and all enumerated types with
                                                    an underlying type of Byte, SByte, Int16, UInt16, Int32, or UInt32.
 
  For type fields, the dynamic memory required to hold the field’s data is allocated inside the type object, which is created when the type is loaded into an AppDomain, which typically happens the first time any method that references the type is just-in-time (JIT)–compiled. 
  
  For instance fields, the dynamic memory to hold the field is allocated when an instance of the type is constructed.
   
  Because fields are stored in dynamic memory, their value can be obtained at runtime only. Fields also solve the versioning problem that exists with constants. In addition, a field can be of any data type, so you don’t have to restrict yourself   to your compiler’s built-in primitive types (as you do for constants).

CLR via C# 3rd - 07 - Constants and Fields的更多相关文章

  1. CLR via C# 3rd - 05 - Primitive, Reference, and Value Types

    1. Primitive Types        Any data types the compiler directly supports are called primitive types. ...

  2. 7.Constants and Fields

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

  3. CLR via C# 3rd - 08 - Methods

       Kinds of methods        Constructors      Type constructors      Overload operators      Type con ...

  4. CLR via C# 3rd - 06 - Type and Member Basics

    1. Different Kinds  of Type Members        A type can define zero or more of the following kinds of ...

  5. CLR via C# 3rd - 04 - Type Fundamentals

    1. System.Object        The runtime requires every type to ultimately be derived from the System.Obj ...

  6. CLR via C# 3rd - 01 - The CLR's Execution Model

    1. Assemly       A managed module is a standard 32-bit Microsoft Windoes portable executable (PE32) ...

  7. CLR via C# 3rd - 03 - Shared Assemblies and Strongly Named Assemblies

    1. Weakly Named Assembly vs Strong Named Assembly        Weakly named assemblies and strongly named ...

  8. CLR via C# 3rd - 02 - Building, Packaging, Deploying, and Administering Applications and Types

    1. C# Compiler - CSC.exe            csc.exe /out:Program.exe /t:exe /r:MSCorLib.dll Program.cs       ...

  9. CLR via C#(07)-静态类,分部类

    一.      静态类-Static 静态类是一些不能实例化的类,它的作用是将一些相关的成员组合到一起,像我们常见的Math, Console等.静态类由static关键字标识,静态类成员也只能是st ...

随机推荐

  1. hdoj 1576

    //1Y真是爽啊 题意:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).  分析:根据题意a=b*x   a=m ...

  2. OC NSString 基本操作(用到补充持续更新)

    1.将字符串拆分成数组 NSString *string = @"1,2,3,4"; NSArray *array = [string componentsSeparatedByS ...

  3. input框只允许输入数字 --------20160705

    //jquery方法 var num = $(this).val(); num = parseInt(num); if(!num){ $(this).html(''); } $(this).val(n ...

  4. windows8.1下javaweb环境搭建及基本配置(jdk+tomcat+eclipse)

    1.下载安装jdk在无空格的路径下,否则在linux下可能出问题.配置环境变量: a.新建系统变量——JAVA_HOME,值——D:\programming\java\jdk8 // win8下若建为 ...

  5. 用PowerMock mock static方法

    在编写代码时,经常需要调用别人已经写好的工具类,而这些工具提供的方法经常是static方法,在这里,直接贴出<PowerMock实战手册>中的例子 待测试方法: public class ...

  6. sqlserver 2008 孤立用户解决方法

    从别一台服务器上得到一个数据库备份.还原到本地,数据库中的用户无法登录,也就是联机帮助中说的还原备份可能产生的孤立用户问题. 一.新建一个 MyDataBase 数据库 二.把备份文件放到 C 盘根目 ...

  7. excel表数据对比 个人收集

    做了那么久猿,转行做测试以后居然折堕到要用excel来对比数据...~—~.真是人算不如天算...不过没关系,技多不压身. 首先,准备好两个对比的数据表,sheet1 跟sheet2 .在sheet1 ...

  8. 简单方便的div垂直居中。

    此处讨论的是,在一个父容器中只有一个DIV,这个DIV相对于父元素垂直居中的问题: 以下列举三种方式:这里为了层次清晰,都是采用的sass写法. 一:适用于子元素有具体的宽高 .wrap{ //父元素 ...

  9. struct与union字节大小的终极解释

    1.字节对齐的细节和编译器实现相关,但一般而言,如在windows下,就VC而言,满足一下三个准则:1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除:2) 结构体每个成员相对于结构体首地址 ...

  10. 【转】java中volatile关键字的含义

    java中volatile关键字的含义   在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言 ...