Friday, July 16, 2010

.NET Compile-Time constants vs. Run-Time constants

There are two types of constants in .NET (C# to be more specific)

  • Compile-Time constants are defined using the keyword "const"

  • Run-Time constants are defined using the keyword "readonly"



Compile-Time constants are relatively faster than run-time constants but are less flexible. Generally speaking run-time constants are better than compile constants despite the fact that they might be slightly slower.



//Compile-Time constant
public const int Pi=3.14;
//Run-Time constant
public static readonly float Pi=3.14;



Compile-Time constants are resolved at compile time, and their value will be placed directly in the code, even across the assemblies.

No comments:

Post a Comment