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. LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1

    思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...

  2. linux与php时间函数有关的错误解决

    最近在程序里写了不少获取时间或时间戳的函数date() strtotime()等,但是把程序拿到linux上运行却爆出这些函数的错误,具体原因是因为linux本身的时间设置以及php的时区问题. 先确 ...

  3. 计算机网络(11)-----TCP连接的建立和释放

    TCP连接的建立和释放 概述 TCP运输连接的建立和释放是每一次面向连接的通信中必不可少的过程,运输连接有三个阶段:连接建立,数据传送和连接释放. TCP连接的建立 如图所示,假定A主机是客户端程序, ...

  4. 如何隐藏DOM元素

    在CSS中,要隐藏DOM元素常见的方法有: 设置元素的opacity值为0 设置元素的visibility值为hidden 设置元素的display值为none 设置元素的position值为abso ...

  5. keep the bar green to keep the code clean——Junit详解(二)

    测试用例&测试套件 举个栗子: 编写MyStack类模拟栈,并对其进行测试用例编写测试: 编写文件删除方法,并对其删除测试. 不再做演示,戳此获取代码 MyStack类: public cla ...

  6. React Native 一个组件styles BUG

    'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...

  7. 研二下学期做的第一个项目(主要关于datagridview的一些笔记)

    首先是行标题列rowheader dataGridView1.TopLeftHeaderCell.Value = "details"; ______________________ ...

  8. rpc使用JUnit模块测试设计的方法及常见问题

    RPC:Remote Procedure Call 远程过程调用 Wikipedia:http://en.wikipedia.org/wiki/Remote_Procedure_Call 百度百科:h ...

  9. 火狐和IE浏览器的兼容问题汇总

    1.window.event code=(navigator.appName="Netscape")?event.which:event.keycode; 2.event.x mx ...

  10. 【1】springmvc4 + servlet3 零配置全注解入门项目helloword

    自从servlet3.0支持注解,使得javaweb项目使用全注解成为可能. 注解化将成为javaweb的发展方向.包括spring去年推出的spring-boot也是全部使用注解. 代码:https ...