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. Backbone的一点使用心得

    Backbone的其实感觉上上手很难,大概在一年前就想实践下,结果总是没有付诸行动,这次需求中狠狠心决定一定要使用一次看看,感受下. 可是第一步真的比较困难,因为直接看API好像没有感觉就在网上找实例 ...

  2. js中,全局变量与直接添加在window属性的区别

    在js中定义的全局变量是挂在window下的,而window的属性也一样,那么这两者有什么区别呢? 其实这两者还是有小小的区别的,全局变量是不能通过delete操作符删除的,而直接定义在window上 ...

  3. AWS-CDH5.5安装-安装

    1.安装MySQL [root@ip---- mysql]# rpm -ivh MySQL-server--.el6.x86_64.rpm MySQL-client--.el6.x86_64.rpm ...

  4. wpf学习笔记

    1.菜单:普通菜单.上下文菜单(ContextMenu) <Menu HorizontalAlignment="Left" Height="20" Ver ...

  5. golang中不定参数与数组切片的区别

    package main import "fmt" func main() { myfunc1(, , , ) //传递不定数量的参数 myfunc2([], , , }) //传 ...

  6. 纯js开发防win7日历控件

    不久前项目开发中遇到需要用js实现选择日期的需求,百度了下,确实一大把一大把的,但多少还是有些不符合当前需求,遂down了一份最接近的,然后修修改改,基本符合了... 先上几张效果图~~~ 需要输入时 ...

  7. Command Pattern

    当(客户)对象访问(服务)请求服务时,最直接的方法就是方法调用.

  8. 简述Git(Linux、Android~~开源)

    Git——源代码管理软件,Android及Linux内核,驱动开发的过程中涉及的大量的源代码,都由Git管理 (一)安装Git Ubuntu Linux10.10或更新的版本,使用下面命令来安装Git ...

  9. java并发包:线程池 executorservice

    1.newCachedThreadPool()  -缓存型池子,先查看池中有没有以前建立的线程,如果有,就reuse.如果没有,就建一个新的线程加入池中 -缓存型池子通常用于执行一些生存期很短的异步型 ...

  10. 5-26课堂作业——组员投票Alpha版存在的问题

    我们在课上讨论了Alpha版目前发现的问题,并通过投票的方式,选出其中三个认为是当前须紧要解决的问题. 问题: 1.缺少数据库: 2.用户体验不良好,游戏难度分区不明显: 3.团队成员共同工作时间少.