Hi Guys
NP80 compile time & runtime
Following paragraph I got from a book. It is explaining about Read-Only field. Please explain the different between the compile time and the runtime.
Thank you
Static Read-Only Fields
Static read-only fields are also permissible. This can be helpful if you wish to create a
number of constant values bound to a given class. In this light, “readonly” seems to be a close cousin to the “const” keyword.
The difference is that the value assigned to “const” must be resolved at compile time, and therefore cannot be assigned a new type instance (as this is computed at runtime). The value of read-only static fields, however, may be computed at runtime, and therefore may be assigned type instances as well as simply data types (int, float, string, etc.).
Posted Feb 12, 2008, 12:34 PM
Thank you, Alan.
AlanPosted Feb 12, 2008, 10:25 AM
The advantages of compile time computation are that:
(1) It improves runtime performance (as there's less work to do), at the expense of taking longer to compile.
(2) It reduces the scope for runtime errors by picking up potential problems at compile time.
On today's hardware, the effect of (1) is likely to be minimal but (2) is always worthwhile because the more bugs you can identify before the program is shipped to the user the better :)
The only advantage I can think of for runtime computation is that it enables the use of values or the results of other computations which are not available at compile time.
Posted Feb 12, 2008, 8:23 AM
What are the advantages of computing some type of data in compile time and some type of data in runtime?
Posted Feb 12, 2008, 6:42 AM
Thank you for your explanation, Alan.
AlanPosted Feb 12, 2008, 6:38 AM
Compile time is when the program's source code is being compiled by the C# compiler into Common Intermediate Language (CIL).
Run time is when the program's CIL is being run by the Common Language Runtime (CLR) and converted 'on the fly' into native code by the 'Just in Time' (JIT) compiler.
Posted Feb 12, 2008, 5:59 AM
What is compile time? and What is the runtime?
AlanPosted Feb 12, 2008, 5:05 AM
The C# compiler is able to do some limited computations provided these only involve 'literals' (such as 5, 6.0, 7.5m, true, "maha", null), enumeration members (which are essentially integers) and other constants.
Where an expression can be computed at compile time, it can be assigned to a 'const' member. An expression for this purpose can be just a simple literal such as 8 which, of course, has an int value of 8.
Otherwise, you have to declare the member as 'readonly' and allow it to be computed at run time. For example, it's not possible (for obvious reasons) for the compiler to do a computation which involves it creating new objects and so anything involving the 'new' operator, but which otherwise remains the same, needs to be defined as 'readonly' rather than 'const'.