PC Review


Reply
Thread Tools Rate Thread

beginner's question: const vs readonly

 
 
R.A.M.
Guest
Posts: n/a
 
      23rd Jun 2006
Hello,
I am learning C#.NET and I donst understand difference between meaning
and usage of "readonly" and "const" keywords. Could you explain me
please.
Thank you
/RAM/
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      23rd Jun 2006
Const expressions are evaluated at compile time for all assemblies using
them

Readonly expressions are evaluated at runtime either in the initializer
(readonly int 5 = {initializer}, or in the constructor; after the
constructor they cannot be changed. This can be useful to avoid accidental
updates to the field, or where the "const" expression can only be evaluated
at runtime - e.g. from a database of config file.

Note (more abstract) also that if assembly A references B, and B has a const
that A uses, then swapping B with an updated const value will *not* reflect
in A, as A's value is compiled already; with consts it *will* show sooner,
but it needs a runtime eval (once).

Also - you can have readonly fields at both the static and instance level,
but consts are (essentially) static. As an example of a readonly instance
field that varies per instance, consider the decorator / facade wrapper
pattern that provides alternative access to the members of an existing
object (e.g. the following that converts methods on the base into properties
on the decorator) - it rarely makes sense to change the base object of such
a wrapper, so it can be readonly.

public class SomeDecorator {
private readonly SomeOtherClass _base;
public SomeDecorator(SomeOtherClass base) {
_base = base;
}
public string SomeProperty {
get {return _base.SomeComplexMethod();}
set {_base.UpdateSomething(value);}
}
}


 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      23rd Jun 2006
"Marc Gravell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Note (more abstract) also that if assembly A references B, and B has a
> const that A uses, then swapping B with an updated const value will *not*
> reflect in A, as A's value is compiled already; with consts it *will* show
> sooner, but it needs a runtime eval (once).


Interesting. (And you mean readonly in the last sentence, I assume.)

///ark


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      24th Jun 2006
Yes, I meant readonly (a public field or property); my bad...

Marc

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and readonly puzzlecracker Microsoft C# .NET 6 1st Oct 2008 11:33 AM
const and readonly TonyJ Microsoft C# .NET 5 20th Feb 2008 02:00 PM
const or readonly Axel Dahmen Microsoft C# .NET 3 23rd Aug 2005 04:28 AM
What is the difference between: readonly and const My4thPersonality Microsoft C# .NET 13 10th Apr 2005 05:53 PM
Difference between const and readonly Amit Microsoft VB .NET 2 27th Feb 2005 05:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:07 AM.