1. Constants

Aconstantis 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,andString. However, C# also allows you to define a constant variable of a non-primitive type if you set the value tonull
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).

转载于:https://www.cnblogs.com/junier/p/3578400.html

CLR via C# 3rd - 07 - Constants and Fields相关推荐

  1. CLR Via C# 3rd 阅读摘要 -- Chapter 24 – Runtime Serialization

    Serialization/Deserialization Quick Start 序列化是将一个对象以及相关的对象转换成字节流的过程:反序列化就是序列化的逆过程: System.Runtime.Se ...

  2. CLR Via C# 3rd 阅读摘要 -- Chapter 28 – Primitive Thread Synchronization Constructs

    Class Libraries and Thread Safety 线程同步是用来避免多个线程同时访问共享数据时出现冲突: 线程同步的障碍: 1.极其乏味易错: 2.锁严重影响性能: 3.线程同步锁在 ...

  3. 《CLR via C#》 第三版的主要内容

    Jeffrey Richter 在他的博客中叙述了 CLR via C#第三版的主要内容,和改动.原本如下:(必要的地方加了翻译) Last week I submitted the reaming ...

  4. CLR via C#, Third Edition图书推荐及下载

    我们先来看看这书的作者Jeffrey Richter,Jeffrey Richter是一位在全球享有盛誉的技术作家,尤其在Windows/.NET领域有着杰出的贡献.他的第一本Windows著作Win ...

  5. 跟小静读CLR via C#-开篇及目录

    最近正在拜读Jeffrey大师的.NET巨作-<CLR via C#>.好书就像一杯茶,需要静下心来慢慢品.在写笔记的过程中,也加入了自己的思考:在和园友们的交流中,理解也不断的加深. 目 ...

  6. abap程序(成本分析报表)

    最近做的一个成本分析的报表. 以下是部分关键代码,仅作参考,just 而已! *&------------------------------------------------------- ...

  7. 一次.net托管内存泄露分析

    简介:一次.net托管内存泄露分析 最近协助分析了一个.net进程内存泄露的问题,过程分享给大家. 症状:客户的服务端.net进程出现分钟级的cpu抖动,接近100%后落回. 图1 分析:支持同学通过 ...

  8. CVPR 2023的Workshop汇总

    作者 | CV君  编辑 | 极市平台 点击下方卡片,关注"自动驾驶之心"公众号 ADAS巨卷干货,即可获取 点击进入→自动驾驶之心[全栈算法]技术交流群 导读 本文总结了本次CV ...

  9. linux sh/bash 编程常用

    本系列针对Bash Shell,其他的比较流行的Shell还有Korn shell (ksh)和"Tenex C shell" (tcsh ). 一 简单过程 1)可以使用任意一种 ...

  10. [.Net 多线程处理系列专题七——对多线程的补充

    因为有些人可能会疑惑,讲了这么多 多线程,到底在实际的应用上有什么作用的呢? 这里我在这里用多线程简单实现了一个文件的下载的功能. 服务器端页面: <%@ Page Language=" ...

最新文章

  1. keras中conv2d,conv2dTranspose的Padding详细介绍
  2. ICCV 2021 Oral | PoinTr:几何敏感的多样点云补全Transformer
  3. 人人都可以做深度学习应用:入门篇
  4. 十八、对已经找到轮廓的图像进行测量
  5. Java学习笔记13--比较器(Comparable、Comparator)
  6. 练习题︱基于今日头条开源数据(二)——两款Apriori算法实践
  7. oracle pl/sql发送邮件多个收件人问题
  8. Android String占位符功能
  9. PhpStorm更换主题
  10. vue面试常见问题小结
  11. 输入两个正整数m和n,求其最大公约数和最小公倍数(常见的错误代码分析)
  12. 2022-2027年中国百元酒店行业市场全景评估及发展战略规划报告
  13. unity2.5D动画学习笔记(一):添加人物四向移动动画及脚本
  14. 如何在AI(Adobe illustrator)里用角标
  15. PHP+TP框架实现微信公众号开发之发送模板消息
  16. 十六进制数据的CRC16校验码自写软件
  17. VBA - 粘贴为数值型
  18. 基于平滑、差分的矩形波零漂(基线漂移)消除算法(MATLAB实现,代码和数据见CSDN同名资源)
  19. Linux学习笔记(一) -- 在虚拟机中安装Ubuntu 18.04
  20. 微星 b460m 10400f 黑苹果 (mac os11) rx570 (B460M-A PRO) efi

热门文章

  1. 求解偏微分方程开源有限元软件deal.II学习--Step 1
  2. 力扣——合并K个排序链表
  3. Kafka集成Kerberos之后如何使用生产者消费者命令
  4. ios网络相关问题-HTTP特点
  5. 对Python的初认识以及期待
  6. [转]mysql_connect() 不支持 请检查 mysql 模块是否正确加载 解决
  7. Fiddler-弱网测试设置
  8. ELK pipeline
  9. iPhone发展【一】从HelloWorld开始
  10. 【转】Javabyte[]数组和十六进制String之间的转换Util------包含案例和代码