C# const常量详细介绍

开发 后端
这里介绍C# const常量,使用Visual C#在 Main()里面使用IntelliSence插入Constant的相关field的时候,发现ReadonlyInt和 InstantReadonlyInt需要指定Constant的实例对象。

C#语言有很多值得学习的地方,这里我们主要介绍C# const常量,包括介绍readonly和const所修饰的变量等方面。

一般情况下,如果你需要声明的常量是普遍公认的并作为单个使用,例如圆周率,黄金分割比例等。你可以考虑使用C# const常量,如:public const double PI = 3.1415926;。如果你需要声明常量,不过这个常量会随着实际的运行情况而决定,那么,readonly常量将会是一个不错的选择,例如上面***个例子的订单号Order.ID。

另外,如果要表示对象内部的默认值的话,而这类值通常是常量性质的,那么也可以考虑const。更多时候我们对源代码进行重构时(使用Replace Magic Number with Symbolic Constant),要去除魔数(Magic Number)的影响都会借助于const的这种特性。

对于readonly和const所修饰的变量究竟是属于类级别的还是实例对象级别的问题,我们先看看如下代码:

using System;  
 
namespace ConstantLab  
{  
class Program  
{  
static void Main(string[] args)  
{  
Constant c = new Constant(3);  
Console.WriteLine("ConstInt = " + Constant.ConstInt.ToString());  
Console.WriteLine("ReadonlyInt = " + c.ReadonlyInt.ToString());  
Console.WriteLine("InstantReadonlyInt = " + c.InstantReadonlyInt.ToString());  
Console.WriteLine("StaticReadonlyInt = " + Constant.StaticReadonlyInt.ToString());  
 
Console.WriteLine("Press any key to continue");  
Console.ReadLine();  
}  
}  
 
class Constant  
{  
public Constant(int instantReadonlyInt)  
{  
InstantReadonlyInt = instantReadonlyInt;  
}  
 
public const int ConstInt = 0;  
 
public readonly int ReadonlyInt = 1;  
 
public readonly int InstantReadonlyInt;  
 
public static readonly int StaticReadonlyInt = 4;  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

使用Visual C#在 Main()里面使用IntelliSence插入Constant的相关field的时候,发现ReadonlyInt和 InstantReadonlyInt需要指定Constant的实例对象;而ConstInt和StaticReadonlyInt却要指定 Constant class(参见上面代码)。可见,用const或者static readonly修饰的常量是属于类级别的;而readonly修饰的,无论是直接通过赋值来初始化或者在实例构造函数里初始化,都属于实例对象级别。

一般情况下,如果你需要表达一组相关的编译时确定常量,你可以考虑使用枚举类型(enum),而不是把多个C# const常量直接嵌入到class中作为field,不过这两种方式没有绝对的孰优孰劣之分。

using System;  
 
enum CustomerKind  
{  
SuperVip,  
Vip,  
Normal  
}  
 
class Customer  
{  
public Customer(string name, CustomerKind kind)  
{  
m_Name = name;  
m_Kind = kind;  
}  
 
private string m_Name;  
public string Name  
{  
get { return m_Name; }  
}  
 
private CustomerKind m_Kind;  
public CustomerKind Kind  
{  
get { return m_Kind; }  
}  
 
public override string ToString()  
{  
return "Name: " + m_Name + "[" + m_Kind.ToString() + "]";  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

【编辑推荐】

  1. C# lock关键字叙述
  2. C#.Net FrameWork简介
  3. C# new和override简单描述
  4. C#值类型和引用类型浅谈
  5. C#标识符简单分析
责任编辑:佚名 来源: CSDN论坛
相关推荐

2009-08-27 15:17:40

C# const变量

2009-08-10 16:30:56

C# BitmapDa

2009-08-12 15:34:40

C# DBNull

2011-06-21 10:37:56

const

2009-08-21 15:16:23

C#使用指针

2009-08-24 18:21:23

C# ListView

2009-08-03 18:49:17

C#和Java

2009-08-07 16:10:20

C#调用API

2009-08-20 15:26:42

C#循环语句

2009-08-21 09:23:11

C# GDI+

2009-08-13 13:38:30

C#命名规范

2009-08-14 17:04:50

C#类型系统

2009-08-27 14:32:15

C#编写ActiveX

2009-08-25 17:28:23

C#创建DataSet

2009-08-06 14:59:36

C#编译器

2009-08-13 15:40:28

C#基础知识

2009-08-27 17:31:44

C#创建Windows

2011-06-08 13:35:18

C#数据类型

2017-10-30 16:50:41

Linuxconst

2011-07-20 16:57:05

C++const
点赞
收藏

51CTO技术栈公众号