The difference between the C # readonly and const

The difference between const, readonly, static readly of

First, that the most important difference: const value is determined at compile time, so the only constant expression specified by its value at the time of declaration. Readonly is determined and its value at runtime.

We all know that they are one and the same point: the three constants defined in specifying the initial value will not be changed, read-only can not write.

In addition there are a few differences and to pay attention to the following areas:

1.const only be declared primitive types, enumerated types, string type. readonly is unlimited;
2.const born as static data, no need to add static identity;
3.cosnt more efficient, more flexible readonly. In the application in place of static readonly const, const in order to balance the lack of flexibility, while overcoming performance cosnt compiler optimizations, brought assembly reference inconsistencies;
4. Reference for a readonly type of, but not limited assignment (writing) operation only. While its members still read and write is unrestricted.

Guess you like

Origin www.cnblogs.com/halfsaltedfish/p/11416168.html