const, static and readonly
const, static and readonly
http://tutorials.csharp-online.net/const,_static_and_readonly
Within a class, const
, static
and readonly
members are special in comparison to the other modifiers.
const
vs. readonly
const
and readonly
perform a similar function on data members, but they have a few important differences.
const
A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the const
keyword and must be initialized as they are declared. For example;
public class MyClass
{
public const double PI = 3.14159;
}
PI
cannot be changed in the application anywhere else in the code as this will cause a compiler error.
Constants must be a value type (sbyte
, byte
, short
, ushort
, int
, uint
, long
, ulong
, char
, float
, double
, decimal
, or bool
), an enumeration, a string literal, or a reference to null
.
Since classes or structures are initialized at run time with the new
keyword, and not at compile time, you can't set a constant to a class or structure.
Constants can be marked as public
, private
, protected
, internal
, or protected internal
.
Constants are accessed as if they were static fields, although they cannot use the static
keyword.
To use a constant outside of the class that it is declared in, you must fully qualify it using the class name.
readonly
A read only member is like a constant in that it represents an unchanging value. The difference is that a readonly
member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared. For example:
public class MyClass
{
public readonly double PI = 3.14159;
}
or
public class MyClass
{
public readonly double PI;
public MyClass()
{
PI = 3.14159;
}
}
Because a readonly
field can be initialized either at the declaration or in a constructor, readonly
fields can have different values depending on the constructor used. A readonly
field can also be used for runtime constants as in the following example:
public static readonly uint l1 = (uint)DateTime.Now.Ticks;
Notes
readonly
members are not implicitlystatic
, and therefore thestatic
keyword can be applied to areadonly
field explicitly if required.
- A
readonly
member can hold a complex object by using thenew
keyword at initialization.
static
Use of the static
modifier to declare a static
member, means that the member is no longer tied to a specific object. This means that the member can be accessed without creating an instance of the class. Only one copy of static
fields and events exists, and static
methods and properties can only access static
fields and static
events. For example:
public class Car
{
public static int NumberOfWheels = ;
}
The static
modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes.
static
members are initialized before the static
member is accessed for the first time, and before the static
constructor, if any is called. To access a static
class member, use the name of the class instead of a variable name to specify the location of the member. For example:
int i = Car.NumberOfWheels;
const, static and readonly的更多相关文章
- 修饰符const,static与readonly
在c语言中,存储区可以分成代码区,全局区(用于存放全局变量和静态变量),常量区(用户存放常量),栈,堆. 首先介绍const,const是常量的标志,表示变量不可被修改.const变量,申明的时候就必 ...
- c#中const、static、readonly的区别
1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...
- C# const和statci readonly区别
1.const 是属于编译时的变量,它定义的常量是在对象初始化时赋值,以后不能改变他的值. 它适用于两种场景:1.取值永久不变(比如圆周率.一天包含的小时数.地球的半径等) 2.对程序性能要求非常苛 ...
- C#枚举(enum)、常量(const)和readonly
const修饰的是(类)静态常量,,其值是在编译期间确定的readonly修饰的是动态常量. A.C#中的const和readonly的区别 C#中定义常量有两种方式,一种叫做静态常量,使用“cons ...
- const,static,extern 简介
const,static,extern 简介 一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编 ...
- 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)
李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...
- const ,static,inline
const: 1 定义变量 ,如下写法都可以: TYPE const ValueName = value; const TYPE ValueName = value; ...
- 类内const static(static const)成员变量初始化问题
在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...
- 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员
转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...
随机推荐
- Qt5 添加右键菜单简单测试
1.在.h文件中包含相关头文件 #include <QMenu> #include <QContextMenuEvent> 2.在.h文件中定义动作对象 QAction *ed ...
- UIImage加载图片的两种方法区别
Apple官方的文档为生成一个UIImage对象提供了两种方法加载图片: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路 ...
- SQLite 字段类型
一般数据采用的固定的静态数据类型,而SQLite采用的是动态数据类型,会根据存入值自动判断. SQLite具有以下五种数据类型: 1.NULL:空值. 2.INTEGER:带符号的整型,具体取决有存入 ...
- matlab 已知函数值纵坐标值(Y值)获得对应的横坐标
clear all;clc; x=-pi/2:pi/50:pi; y=sin(x); plot(x,y); grid on; fm=max(y) id=find(y==fm); xm=x(id) 转自 ...
- 优秀HTML5活动页面
一个好的手机活动宣传 更能让人分享 传播是爆炸性的 下面是我平时看到一些好的微信活动宣传页面 分享给大家 其中用到的技术 常做微信活动 专题页面的人 可以看看大神们是怎么做的 这样到自己做的时候 ...
- leetcode 136. Single Number ----- java
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- JavaWeb学习记录(二十七)——定时发送邮件ServletContextListener监听实现
public class EmailSendListener implements ServletContextListener{ @Override public void contextDe ...
- mysql中slow query log慢日志查询分析
在mysql中slow query log是一个非常重要的功能,我们可以开启mysql的slow query log功能,这样就可以分析每条sql执行的状态与性能从而进行优化了. 一.慢查询日志 配置 ...
- hihoCoder#1107 : Shortest Proper Prefix (前缀树)
题目大意:在n个单词中,如果以s作为前缀的单词个数不超过5个,那么称s为proper prefix.如果s为proper prefix并且s的任何一个前缀(不包括s)都不是proper prefix, ...
- IOS请求H5页面、要求自定义agent判断是电脑、安卓还是iPhone登录
//自定制的userAgent- (void)createMyAgent{ NSString *userAgent = [[[UIWebView alloc]init]stringByE ...