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. 基于linux运用python开发知识点滴

    我是小白,希望我的文章能对小白们有点作用. A.Linux的开源,优势明显,如何使用,基本命令如下: 个人认为最基础的两种操作: 1.文件操作: ls 看文件夹下内容 ls -a 隐藏文件 -l非隐藏 ...

  2. Nunit 使用介绍

    Nunit是.NET平台单元测试框架,其是从Junit发展而来,它强大之处是支持所有的.NET语言. Nunit的下载地址:http://www.nunit.org 介绍1: 布局: 左面:我们写的每 ...

  3. Inherits、CodeFile、CodeBehind的区别

    Inherits.CodeFile.CodeBehind 在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. 通常一个 @pa ...

  4. 【Sublime Text 3】插件

    TrailingSpacer 高亮显示多余的空格和Tab HTML-CSS-JS Prettify

  5. Kubuntu麦克风音频无声音

    前段时间买了新本,装了双系统,win8和kubuntu 14.04,使用的过程感觉都不错,因为平时玩游戏看视频是用win8,但最近打算在kubuntu上听音乐时,发现音频没有声音,麦克风也没有声音,这 ...

  6. linux,下载与安装

    1.下载地址 1.网易开源镜像站 http://mirrors.163.com 2.centos 官方站 http://www.centos.org 2.虚拟机 VirtualBox  ------- ...

  7. [转]CentOS6.3安装JDK和环境配置

    转自:http://www.linuxidc.com/Linux/2012-09/70780.htm 1.CentOS默认情况下,会安装OpenOffice之类的软件,这些软件需要Java的支持,默认 ...

  8. Linux1.0源代码编译过程

    根据源代码包中的readme文件及http://chfj007.blog.163.com/blog/static/173145044201191195856806/?suggestedreading& ...

  9. 关于HTML的Element

    今天搞HTML的时候,发现了一些操作element的方法.先引用一篇. 1.document.getElementById(id);  2.document.getElementByTagName(t ...

  10. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...