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. spark 1.5.2配置记录

    1)slaves # A Spark Worker will be started on each of the machines listed below. dataNode 2)spark-env ...

  2. 使用BCrypt算法加密存储登录密码用法及好处

    //导入import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /** *使用BCrypt算法加密存储登录密码 ...

  3. Android ShapeDrawable

    今天做项目碰到一个这样的情况,就是颜色指示框,用的是正方形边框是黑色的,里面填充颜色,颜色值是动态的,为了解决这个问题,查了好多资料,终于找到解决的方法,利用ShapeDrawable,我们自定义一个 ...

  4. 张小龙微信小程序演讲内容简介

    1月9号,看看实际上如何

  5. oracle 归档日志

    归档日志(Archive Log)是非活动的重做日志备份.通过使用归档日志,可以保留所有重做历史记录,当数据库处于ARCHIVELOG模式并进行日志切换式,后台进程ARCH会将重做日志的内容保存到归档 ...

  6. SpellTime

    如果你的应用程序允许用户输入文本,或者它结合了任何基于文本的处理,那么我们有一款你一直寻找的产品.Spell Time 允许你把个拼写检查器整合到你的产品中.该产品携带了完整的源码.Spell Tim ...

  7. ueditor 图片上传功能(.net)

    假如下载的net文件 所在工程引用bin文件中的Newtonsoft.Json.dll 在浏览器中运行 `net/controller.ashx`,如果返回 "`{"state&q ...

  8. 《java异常的一些总结》

    关于Java中异常的一些总结: 3 有些时候,程序在try块里打开了一些物理资源(例如数据库连接,网络连接. 4 和磁盘文件等),这些物理资源都必须显示回收. 5 6 注意:Java的垃圾回收机制不会 ...

  9. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  10. xamarin for android 生成时“java.exe已退出 代码为1”

    Xamarin Studio中创建了一个Android Application 运行时出现错误:C:\Program Files\MSBuild\Xamarin\Android\Xamarin.And ...