Thursday, February 07, 2013

What is the difference between const and readonly?

Posted by Rahul Kharde at 7:55 AM


Const
Readonly
A constant value will be embedded in the executable file at compile time, 
A read only variable is read by the program at run time. 
Initiailized at declaration only.
Can be initialized in declaration or by code in the constructor.
Can't be static.
Can be either instance-level or static.
Value is evaluated at compile time.
Value is evaluated at run time.

Readonly Example

public class ClassA
{
    public readonly double PI;

    public ClassA()
    {
        PI = 3.14159;
    }
}


Const Example

public class ClassA
{
    public const double PI = 3.14159;
}



If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top