In C# it means both more and less that simply that the value won't be
modified.
More, because it actually has to be a compile-time literal, and the compiler
is allowed to substitute the literal value everywhere the const variable
appears. In essence, the const field exists only in the metadata.
Less, because you can't mark anything as "const" except static fields,
unlike C++ where variables and access through pointers can be marked "const"
(can't modify the value) anywhere they appear, including function arguments
and even the "this" pointer.
C#'s "readonly" comes a lot closer to a simple marker "cannot be modified
after creation".
Less, because you can't mark anything as "const" except static fields,
unlike C++ where variables and access through pointers can be marked "const"
(can't modify the value) anywhere they appear, including function arguments
and even the "this" pointer.
>
I don't think you have to declare a field static if you specify const.
I believe compilers deduce that automatically.
>Less, because you can't mark anything as "const" except static
>fields, unlike C++ where variables and access through pointers can
>be marked "const" (can't modify the value) anywhere they appear,
>including function arguments and even the "this" pointer.
>>
>
I don't think you have to declare a field static if you specify const.
I believe compilers deduce that automatically.
>
correct me if I am wrong.
No, in fact you can't use the "static" and "const" keywords together. But
whenever you use "const", you get a static member.
On Wed, 22 Oct 2008 07:20:11 -0700, Ben Voigt [C++ MVP]
<rbv@nospam.nos pamwrote:
[...]
Less, because you can't mark anything as "const" except static fields,
unlike C++ where variables and access through pointers can be marked
"const"
(can't modify the value) anywhere they appear, including function
arguments
and even the "this" pointer.
Small nit: you can mark local variables as "const" as well.
And yes, "const" applied to a field implies "static" as well.
On Wed, 22 Oct 2008 07:20:11 -0700, Ben Voigt [C++ MVP]
<rbv@nospam.nos pamwrote:
>
>[...]
>Less, because you can't mark anything as "const" except static
>fields, unlike C++ where variables and access through pointers can
>be marked "const"
>(can't modify the value) anywhere they appear, including function
>arguments
>and even the "this" pointer.
>
Small nit: you can mark local variables as "const" as well.
Even smaller nit: they aren't actually variables at that point (I think the
C# compiler will totally remove local constants during the compile process,
not even leaving metadata). The expression has to be a compile-time
literal, which is quite different from C and C++ where const means you can
have a run-time calculation but the variable can't be changed after it is
initialized.
>
And yes, "const" applied to a field implies "static" as well.
>
Pete
On Wed, 29 Oct 2008 12:24:51 -0700, Ben Voigt [C++ MVP]
<rbv@nospam.nos pamwrote:
>Small nit: you can mark local variables as "const" as well.
>
Even smaller nit: they aren't actually variables at that point (I think
the
C# compiler will totally remove local constants during the compile
process,
not even leaving metadata).
I know what you mean, but it really just depends on what you definition of
"variable" is.
I haven't checked the C# spec, but MSDN definitely still refers to them as
"variables" even when they are "const". I was simply trying to be
consistent with MSDN's terminology.
Comment