Sunday, September 4, 2011

Constants in .NET


There are two types of constants available in c#:
Compile-time constants and runtime constants. They have different behaviors and
using wrong one will cost you performance or correctness.


But Runtime constants are always preferable over
compile-time constants. Though the compile-time constants are slightly faster
but they are not flexible with compare to runtime constants.


The compile-time constants are preferable when performance
is really required and you don’t have any requirements to change the value of
that constant.


Compile-time constant are declared using “const” keyword:


Eg: public const int a=10;


Run-time constants are declared using “readonly” keyword:


Eg: public readonly int a=10;



compile-time – “Const”


run-time – “readonly”


compile-time
constant
can be declared at class level


Run-time
constant
can be declared at class level


compile-time
constant
can be declared at method level


Run-time
constant
Cannot be declared at method level


A compile-time constant is replaced with the value of that
constant in object code.

Eg:

public const int a=10;

int b=20;

if(b==a)

compiles to the same IL as if you had written this:

if(b==10)// means instead of comparing with variable ‘a’ it
will write its value in compiler-generated IL. Which increase speed but for
various situations its not preferable.


Runtime constants are evaluated at runtime. The IL generated
when you reference a read-only constant references the “
readonly “
variable,
not the value.

Eg:

public const int a=10;

int b=20;

if(b==a)

compiles to the same IL as if you had written this:

if(b==a)// so instead of placing its value as like it do with
“const”,  its place variable for “readonly” variables in compiler-generated
IL.


compile-time constant cannot initialize using the new operator,
even when the type being initialized is a value type.


runtime constants can be of any type, They

must
be initialized using constructor, or using initializer.


Compile-time constants can be used only for primitive types
(built-in integral and floating-point types), enums, or strings.

 

These are the only types that enable you to assign meaningful
constant values in initializers.

 

As we know that for “const” type in compiler-generated IL the
variables get replaces with vales so,these primitive types are the only ones
that can be replaced with literal values. in the compiler-generated IL.


runtime
constants can be of any type.

Read-only values are also constants, in that they cannot be
modified after the constructor

has executed. But read-only values are different in that they
are assigned at runtime.


Compile-time
constants are much more better in performance but less flexible with compare
to run-time constants.


Run-time constants are much more flexible but its performance
is slower with compare to compile-time constants.


Compile-time constants are, by definition,

static
constants.


readonly values can be used for instance constants,
storing different values

for each instance of a class type.


Every
time application assembly need to be rebuilt whenever library assembly which
contains constant gets its constant value changed ( as the compiler-generated
IL contains values of compile-time constants instead of referring variable)


No need to rebuild Application assembly which is referring a
library assembly which contains run-time constants whose value is changed and
its assembly is rebuilt.


 


The final advantage of using const over readonly is
performance: Known constant values can generate slightly more efficient code
than the variable accesses necessary for readonly values.
However, any gains are slight and should be weighed against the decreased
flexibility. Be sure to profile performance differences before giving up the
flexibility.



Compiled By: Rajesh Rolen

Share This!


1 comment:

Anonymous said...

You could certainly see your expertise within the work you write.

The world hopes ffor even more passionate writers succh as you who are not afraid to mention how they believe.

At all times go after your heart.

Powered By Blogger · Designed By Seo Blogger Templates