E*******s 发帖数: 42 | 1 what is difference between const and readonly? |
st 发帖数: 1685 | 2 what's the real use for the difference?
I found below URL:
http://www.c-sharpcenter.com/Tutorial/ConstVsReadOnly.htm
【在 E*******s 的大作中提到】 : what is difference between const and readonly?
|
s*i 发帖数: 5025 | 3 Example: Suppose we have an ArrayList. Its filed "Count" is readonly, but
definitly not "const".
【在 st 的大作中提到】 : what's the real use for the difference? : I found below URL: : http://www.c-sharpcenter.com/Tutorial/ConstVsReadOnly.htm
|
a**y 发帖数: 335 | 4 const can not be changed in any case, readonly can not be changed by you. :)
【在 E*******s 的大作中提到】 : what is difference between const and readonly?
|
C****n 发帖数: 2324 | 5 All right guys.
1. Const can not be changed, and can only be declared in the field
declaration.
i.e.: public const int i=5;
2. const automatically means static.
3. Readonly can be asigned in CONSTRUCTOR!
The real world concern:
Never declare a public field as CONST (static), use readonly
instead, which will save you from recompilation if the const in the underlying
library is
recompiled. Only use const with your private field.
Well, a better pratice is never declare public field.
The Count in Ar
【在 a**y 的大作中提到】 : const can not be changed in any case, readonly can not be changed by you. :)
|
st 发帖数: 1685 | 6 well, why you want an arrayList's count field to be readonly?
【在 s*i 的大作中提到】 : Example: Suppose we have an ArrayList. Its filed "Count" is readonly, but : definitly not "const".
|
g*****g 发帖数: 34805 | 7 When you use const, it has to be known by compilation time.
readonly is better, you want to assign the size in initilization.
That size is determined at runtime.
【在 st 的大作中提到】 : well, why you want an arrayList's count field to be readonly?
|
st 发帖数: 1685 | 8 why I ant arrayList count field to be either readonly or const?
【在 g*****g 的大作中提到】 : When you use const, it has to be known by compilation time. : readonly is better, you want to assign the size in initilization. : That size is determined at runtime.
|
s*i 发帖数: 5025 | 9 This is a good explanation. I got the concept wrong.
underlying
of
【在 C****n 的大作中提到】 : All right guys. : 1. Const can not be changed, and can only be declared in the field : declaration. : i.e.: public const int i=5; : 2. const automatically means static. : 3. Readonly can be asigned in CONSTRUCTOR! : The real world concern: : Never declare a public field as CONST (static), use readonly : instead, which will save you from recompilation if the const in the underlying : library is
|
L*******r 发帖数: 1011 | 10 Your explain is clear. //admire
Actually, just think about the reason why C++ introduces const.
Then we will know the basic feature of const le. :)
underlying
of
【在 C****n 的大作中提到】 : All right guys. : 1. Const can not be changed, and can only be declared in the field : declaration. : i.e.: public const int i=5; : 2. const automatically means static. : 3. Readonly can be asigned in CONSTRUCTOR! : The real world concern: : Never declare a public field as CONST (static), use readonly : instead, which will save you from recompilation if the const in the underlying : library is
|
a**y 发帖数: 335 | 11 o. I thought the 'readonly' in the question is an attribute
instead of a data type modifier.
And I didn't know C# has a 'readonly' modifier until now. //blush
【在 C****n 的大作中提到】 : All right guys. : 1. Const can not be changed, and can only be declared in the field : declaration. : i.e.: public const int i=5; : 2. const automatically means static. : 3. Readonly can be asigned in CONSTRUCTOR! : The real world concern: : Never declare a public field as CONST (static), use readonly : instead, which will save you from recompilation if the const in the underlying : library is
|