The difference between Const and ReadOnly

原创 2005年05月16日 12:24:00

在C#中,定义常量有两种方法,一种是使用const,另一种是使用readonly。他们之间是有一定的区别的,在使用的时候一定要小心,选

择适合的一个来用。

总的来说,使用const要比readonly的效率要高一些,因为const的值是在编译时期就定下来了的,而readonly的值要在程序运行时期

才能确定下来。但是readonly要比const在使用上要灵活一些。

const只能用来定义基本类型,枚举或是字符串;而readonly可以用来定义任何类型。

用于const常量的值是在编译时期就定下来了的,所以当一个const常量的值在程序中被修改过之后,所有引用过这个常量的程序都必须重

新编译一次,否则就会就会出现虽然常量的值已经被修改了,但是引用他的其他程序仍然使用老值的错误。而使用readonly常量就不会出现

这种情况,因为它的值是动态解析的。

Just4Fun - Comparaison between const and readonly in C#

Just4Fun - Comparaison between const and readonly in C#

Difference_between_PLMN_and_PSTN

  • 2008年11月06日 10:48
  • 41KB
  • 下载

python中下划线,双下划线,首尾双下划线的区别(Difference between _, __ and __xx__ in Python)

Difference between _, __ and __xx__ in Python When learning Python many people don't really under...
  • Sinchb
  • Sinchb
  • 2013年03月13日 13:24
  • 2871

What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop?

Each program runs in at least one thread. You can think of each thread as a separate process of prog...
  • Koupoo
  • Koupoo
  • 2011年08月15日 11:50
  • 430

What is the basic difference between NSTimer, NSTask, NSThread and NSRunloop ?

(转)http://www.devdiv.com/forum.php?mod=viewthread&tid=38980&extra=page%3D1%26filter%3Dtypeid%26typei...

Difference Between Performance Testing, Load Testing and Stress Testing – With Examples

1) Performance Testing: Performance testing is the testing, which is performed, to ascertain how th...

What is the Difference Between http and https?

Hypertext Transfer Protocol (http) is a system for transmitting and receiving information across the...

In SQL, what is the difference between a left join and a left outer join?

There is actually no difference between a left join and a left outer join – they both refer to the e...

Difference Between FPGA and CPLD

With all the vast improvements in technology, a lot has been happening that most people won’t really...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The difference between Const and ReadOnly
举报原因:
原因补充:

(最多只允许输入30个字)