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 implicitly static, and therefore the static keyword can be applied to a readonly field explicitly if required.
  • A readonly member can hold a complex object by using the new 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的更多相关文章

  1. 修饰符const,static与readonly

    在c语言中,存储区可以分成代码区,全局区(用于存放全局变量和静态变量),常量区(用户存放常量),栈,堆. 首先介绍const,const是常量的标志,表示变量不可被修改.const变量,申明的时候就必 ...

  2. c#中const、static、readonly的区别

    1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...

  3. C# const和statci readonly区别

    1.const 是属于编译时的变量,它定义的常量是在对象初始化时赋值,以后不能改变他的值. 它适用于两种场景:1.取值永久不变(比如圆周率.一天包含的小时数.地球的半径等)  2.对程序性能要求非常苛 ...

  4. C#枚举(enum)、常量(const)和readonly

    const修饰的是(类)静态常量,,其值是在编译期间确定的readonly修饰的是动态常量. A.C#中的const和readonly的区别 C#中定义常量有两种方式,一种叫做静态常量,使用“cons ...

  5. const,static,extern 简介

    const,static,extern 简介 一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 执行时刻:宏是预编 ...

  6. 李洪强iOS经典面试题155 - const,static,extern详解(面试必备)

    李洪强iOS经典面试题155 - const,static,extern详解(面试必备) 一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽 ...

  7. const ,static,inline

    const: 1 定义变量 ,如下写法都可以: TYPE const ValueName = value;                  const TYPE ValueName = value; ...

  8. 类内const static(static const)成员变量初始化问题

    在查找const相关资料的过程中,又遇到了另外一个问题,就是C++类中const static(或者static const)成员变量应当如何初始化的问题. 查阅了许多资料,发现VC环境下,只允许co ...

  9. 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员

    转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...

随机推荐

  1. 头文件定义和ARM指令

    2015.2.2星期一,阴天 内存管理:内存的分配和释放等静态和动态内存:主要是在释放方式上的区别 静态变量:编译时就已经确定,定义在函数外面自动变量:在程序运行时才能在栈中确定只读数据节:存放常量的 ...

  2. linux命令:rm

    1.介绍: rm用来删除文件或者目录,对于链接文件,只删除了链接,不删除源文件.rm是一个非常危险的命令,像rm -rf /这个命令运行后,后果不堪设想. 2.命令格式: rm [选项] 文件/目录 ...

  3. cout 计算顺序问题

    cout输出流的执行顺序   下面是IBM的一道笔试题 #include <iostream> using namespace std; int fun( ) { cout << ...

  4. 如何在magento后台增加一个自定义订单状态

    magento后台订单状态(order status)只有Pending.Processing.On Hold.Closed.Canceled.Pending Payment 等等,如何在magent ...

  5. iOS开发环境C语言基础 运算符和表达式

    1 年龄判断程序 1.1 问题 本案例需要使用交互的方式判断年龄的范围:用户从控制台输入一个年龄,由程序判断该年龄是否在18~50岁之间.程序交互过程如图-1所示: 图-1 1.2 步骤 实现此案例需 ...

  6. 深入理解Linux修改hostname 转

    当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...

  7. angularjs 不同的controller之间值的传递

    Sharing data between controllers in AngularJS I wrote this article to show how it can possible to pa ...

  8. css 包含的图片和style="display:none"可以避免图片加载,可以节省网络流量

    从别人那儿学到一招:先记录下来: <head> <meta charset="UTF-8"> <title>Document</title ...

  9. R处理xml文件

    最近处理数据的时候需要处理一些xml文件,但是xml文件的格式之前并不是很熟悉.幸好R有一些函数可以帮助快速处理xml文件.其中的xmlToList函数尤其方便,可以使你从陌生的XML迅速回到熟悉的R ...

  10. 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...