-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: dnyanesh
Message 3 in Discussion
What is the difference between Constant and Readonly?
The difference is that the value of a static readonly field is set at run time,
and can thus be modified by the containing class, whereas the value of a const
field is set to a compile time constant.
In the static readonly case, the containing class is allowed to modify it only
in the variable declaration (through a variable initializer) in the
static constructor (instance constructors, if it's not static)
static readonly is typically used if the type of the field is not allowed in a
const declaration, or when the value is not known at compile time.
Instance readonly fields are also allowed.
Remember that for reference types, in both cases (static and instance) the
readonly modifier only prevents you from assigning a new reference to the
field. It specifically does not make immutable the object pointed to by the
reference.
class Program<o:p></o:p>
{<o:p></o:p>
public static readonly Test test = new Test();<o:p></o:p>
<o:p> </o:p>
static void <st1:place w:st="on">Main</st1:place>(string[]
args)<o:p></o:p>
{<o:p></o:p>
test.Name = "Program";<o:p></o:p>
<o:p> </o:p>
test = new Test(); // Error: A static readonly field cannot
be assigned to (except in a static constructor or a variable initializer)
<o:p></o:p>
<o:p> </o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
class Test<o:p></o:p>
{<o:p></o:p>
public string Name;<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
On the other hand, if Test were a value type, then assignment to test.Name
would be an error.
Regards,
Dnyaneshwar Parkhe
http://dnyaneshwar.blog-city.com
http://spaces.msn.com/members/dnyanesh
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]