views:

748

answers:

4

In C#, does setting a field as readonly reduce memory usage?

i.e.

DBRepository _db = new DBRepository();

vs

readonly DBRepository _db = new DBRepository();

Just curious. Thanks.

+14  A: 

No. (inserting more characters)

It means you can't assign to it except at the declaration point or in a constructor. You also can't pass it as a ref or out parameter to a method.

Edit: based on the comment below. The following field is readonly because you want callers to have direct read access to string.Empty, but you don't want them to set it to something else.

public sealed class String
{
    public static readonly string Empty = "";
}

Also, sometimes when I have a List<> or Dictionary<> as a field in a class, I'll declare it readonly to indicate that I want to work with its members (even Add and Remove items, etc.) but I never want to actually assign a different List or Dictionary object to it.

One more edit: Make sure you read about the deprecated Path.InvalidPathChars field (in the remarks section) to show a serious problem that can happen when you don't understand what readonly is and is not.

280Z28
A question in a question. If readonly don't reduce memory for what is good political use readonly in fields?
pho3nix
@pho3nix: Erm, how about enforcing design practices? (If a variable shouldn't be modified, don't let it be modified!)
Noldorin
Thanks for the quick answer.
Chad
readonly is about to make you sure that field is in initial state. For example if field is not readonly you need to check if it null. With readonly you can do single check during object creation and then use this field without pain.
Mike Chaliy
@Mike Chaliy - Excellent point! Thank you.
Chad
I added a final edit to point out what `readonly` is *not*.
280Z28
A: 

readonly is useful in the case you want to use an object as an key. This key shouldn't change, otherwise you cannot access what it gives access to.

For instance; 1) with events in an ASP.NET site, you can use the Events collection in a Control. In your control you would keep a readonly object field to refer to the event handlers in the collection. 2) In multi-treading, you might need to synchronize access. This can be done to combine the lock statement with a readonly object on which to lock.

bgever
+4  A: 

No. readonly and const basically serve the same purpose: to prevent assignments to the values at runtime. However, they have semantically different uses.

readonly versus const

A const field is evaluated at compile time and can only be initialized at declaration, while a readonly field is evaluated at run time and can be initialized both in constructors and at declaration site.

Also, a const field value is stored as part of the metadata and so is initialized in the relevant constructor of the class that declared it while readonly fields will have to be computed at runtime.

Mike J
A: 

YES!

Yes? Yes! How? Simple. A read-only field cannot get a new value so you can't create a second DBRepository and assign it to this field. If it's not read-only then you could write code which will re-assign a new value to this field. And during the time between the reassignment and the time for the garbage collector to clear your old value, you will have a bit more memory in use. Furthermore, if cleanup of the DBRepository has a memory leak, then reassigning new values to this field will cause multiple memory leaks.

A situation where such a memory leak could occur is this: 1) You assign a value to _db. 2) You assign the value of _db to another field. 3) You reassign a new value to _db. At this moment you will have two DBRepository objects in memory because the old object is still referenced to from another object. Thus the old object won't be freed until the second field releases it.

But this is very nitpicky and quite rare. The answer "No" is more appropriate. Don't use "ReadOnly" to save memory because you'd be using it for the wrong reason. Use "ReadOnly" to make sure others won't reassign a value to this field. (Basically, I'm saying that it can reduce memory usage...)

Workshop Alex