readonly和const区别
常量和只读变量的区别
const string name="Xuj";
readonly string name;
1、常量是不可改变的,只读变量只能在构造方法中才能改变其值。
2、C#编译器在编译的时候,声明常量的那句话不见了,在使用常量的时候就被常量的值替代了,在些数据在整个程序的运行中其值不会发生改变,我们就把这个值定义为常量,以提高程序的运行效率。
3、readonly是运行时确定值,const是编译时必须确定其值。
Eg:
readonly string qq=System.ConfigrationManager.AppSettings[qq].ToString();//右边的内容只有在运行的时候才会被执行,所以这句话无误
const string qq=System.ConfigrationManager.AppSettings[qq].ToString();//const是在编译时候确定其值的,所以这句话出错。
readonly和const区别的更多相关文章
- C# readonly与const区别
静态常量:是指编译器在编译时候会对常量进行解析,并将常量的值替换成初始化的那个值. 动态常量的值则是在运行的那一刻才获得的,编译器编译期间将其标示为只读常量,而不用常量的值代替,这样动态常量不必在声明 ...
- C#基本知识点-Readonly和Const的区别
什么是静态常量(Const)和动态常量(Readonly) 先解释下什么是静态常量(Const)以及什么是动态常量(Readonly). 静态常量(Const)是指编译器在编译时候会对常量进行解析 ...
- readonly和const的区别
readonly与const的区别1.const常量在声明的同时必须赋值,readonly在声明时可以不赋值2.readonly只能在声明时或在构造方法中赋值(readonly的成员变量可以根据调用不 ...
- c#:readonly与const的区别
readonly与const的区别: 1.初始化:const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 2.值: const 字段是编译时常量(con ...
- 编写高质量代码改善C#程序的157个建议——建议6: 区别readonly和const的使用方法
建议6: 区别readonly和const的使用方法 很多初学者分不清readonly和const的使用场合.在我看来,要使用const的理由只有一个,那就是效率.但是,在大部分应用情况下, “效率” ...
- C# readonly和const的区别
什么是静态常量(Const)和动态常量(Readonly) 先解释下什么是静态常量(Const)以及什么是动态常量(Readonly). 静态常量(Const)是指编译器在编译时候会对常量进行解析,并 ...
- C# 静态常量(const)动态常量(static、readonly)用法区别
C# 常量的命名方法 一个低级错误 readonly.const 代码 使用方式 支持类型 维护性 性能比较 优先使用 C# Static 与 Java Static C# Const 与 Java ...
- 我所理解的readonly和const
最近要给学校软件小组新成员讲几次课,所以把很多以前懒得学习的和模糊不清的知识点,重新学习了一下. MSDN是这样解释的: readonly 关键字与 const 关键字不同. const 字段只能在该 ...
- readonly与const
readonly与const 在C#中,readonly 与 const 都是定义常量,但不同之处在于:readonly 是运行时常量,而 const 是编译时常量. ; public void Te ...
随机推荐
- 使用PowerShell脚本部署定时器到MOSS2010
转:http://www.77site.com/tech/1087042010072906074113_2012050808152911.html 第一章 前言 在此次练习中,您将了解到如何使用Pow ...
- net 内存泄露和内存溢出
一直以来都对内存泄露和内存溢出理解的不是很深刻.在网上看到了几篇文章,于是整理了一下自己对内存泄露和内存溢出的理解. 一.概念 内存溢出:指程序在运行的过程中,程序对内存的需求超过了超过了计算机分配给 ...
- normalization归一化
简单的举个例子:一张表有两个变量,一个是体重kg,一个是身高cm.假设一般情况下体重这个变量均值为60(kg),身高均值为170(cm).1,这两个变量对应的单位不一样,同样是100,对于身高来说很矮 ...
- HW5.16
public class Solution { public static void main(String[] args) { for(int i = 2000; i <= 2010; i++ ...
- HDU5418.Victor and World(状压DP)
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #i ...
- Go Slices: usage and internals
Introduction Go's slice type provides a convenient and efficient means of working with sequences of ...
- JavaScript- The Good Parts CHAPTER 2
I know it well:I read it in the grammar long ago.—William Shakespeare, The Tragedy(悲剧:灾难:惨案) of Titu ...
- smarty对网页性能的影响--开启opcache
在上一篇<smarty对网页性能的影响>中,默认没有开启opcache,于是我安装了一下zend opcache扩展,重新实验了一下,结果如下: 有smarty 用apache的ab命令进 ...
- Sqlite在Windows、Linux 和 Mac OS X 上的安装过程
一:在 Windows 上安装 SQLite 1,下载 请访问SQLite下载页面http://www.sqlite.org/download.html,从Windows 区下载预编译的二进制文件.需 ...
- SGU107——987654321 problem
For given number N you must output amount of N-digit numbers, such, that last digits of their square ...