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成员 今天写程序,出现一个新错误,好 ...
随机推荐
- Python的文件类型
Python的文件类型主要分为3种:源代码(source file).字节码(byte-code file).优化的字节码(optimized file).这些代码都可以直接运行,不需要编译或者连接. ...
- Visual Studio Usage
Navigation Double click on UI element goes to its _click event. F12 – for method or variable, go to ...
- Mercurial使用简单介绍【转】
Mercurial可以使异地开发进行统一的版本管理,比如一个项目有一部分人在银行内部开发无法用外网,另一部分人在公司开发,这两部分人需要一个统一的版本管理工具,mercurial这时候可以发挥作用 m ...
- Tomcat集群---Cluster节点配置(转)
<!-- Cluster(集群,族) 节点,如果你要配置tomcat集群,则需要使用此节点. className 表示tomcat集群时,之间相互传递信息使用那个类来实现信息之间的传递. cha ...
- Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- win live
ok, this is my first live blog; just for test. r U ok fmkf vdsvsvv svsvsvsv try{ }
- C# 入门篇之listview用法
在用listview,经常对各种属性都不了解,用完了,下次又忘:写个随笔记记. 看到别人已经有总结很详细的,就直接转过来了,可能中间会有一些自己加的. ------------------------ ...
- Sed 直接修改文件
sed最常用的用法莫过于替换文件,然而其默认的模式是直接输出在shell中 sed 's/Old/New/' My_File.txt 如果我们想要sed直接在文件中更改,只需要在sed后面添加 -i ...
- EasyUI DateBox 按钮自定义添加功能
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- codeforce Error Correct System
题目大意: 给出两串n(1 ≤ n ≤ 200 000)个字母的字符串, 求出最多交换一对数, 使得不相同对数变少,求出不相同的对数以及交换的数的位置,若不需交换则输出-1,-1. 分析: 用矩阵记录 ...