Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
Adding "C# analog" paragraph |
||
Line 192:
Further, because C and C++ expose pointers and references directly, there is a distinction between whether the pointer itself is constant, and whether the data pointed to by the pointer is constant. Applying <code>const</code> to a pointer itself, as in <code>SomeClass * const ptr</code>, means that the contents being referenced can be modified, but the reference itself cannot (without casting). This usage results in behaviour which mimics the behaviour of a <code>final</code> variable reference in Java. By contrast, when applying const to the referenced data only, as in <code>const SomeClass * ptr</code>, the contents cannot be modified (without casting), but the reference itself can. Both the reference and the contents being referenced can be declared as <code>const</code>.
==C# analog==
C# is considered the language that is too close to Java; Java has JVM, C# has .Net Framework; Java has bytecode, C# has MSIL; Java has no pointers (real memory) support, C# is the same.
Regarding the final keyword, which is the variable that is allowed to be assigned once, C# has two solutions:
For methods and classes use <code>sealed</code>, however for the variables use <code>readonly</code> <ref>[https://stackoverflow.com/questions/1327544/what-is-the-equivalent-of-javas-final-in-c What is the equivalent of Java's final in C#?]</ref>
Note that the difference between <code>const</code> and <code>readonly</code> is that const is evaluated at compile time, however, readonly is evaluated at runtime, thus can has a runtime expression to be calculated and fixed at runtime.
==References==
|