Reputation: 15302
public class Foo
{
private readonly Bar _bar;
}
public class Foo2
{
private Bar _bar;
}
I don't see any benefit to marking it as readonly. It's private and if I try do something internally to modify it it's me being dumb since I know how I want my class to behave? So, what's the point? I don't think there is any performance gain here, so that can't be it.
Upvotes: 1
Views: 426
Reputation: 81327
Suppose a class is supposed to hold a changeable set of T
's, and it has a private field T[] theArray
. There are a number of ways the class could be set up:
The three different approaches have different implications for things like thread-safety. The first can be made thread-safe if the readref-copy-modify-writeref sequence is placed within an Interlocked.CompareExchange
loop. The last may be made thread-safe by making individual element writes thread-safe. The second one, however, may only be made thread-safe by at minimum either having all write operations share a lock which is common to the list as a whole and requiring readers to double-check their operations to ensure consistency, or else requiring that all read and write access must go through a common lock.
Upvotes: 1
Reputation: 9100
It's private and if I try do something internally to modify it it's me being dumb since I know how I want my class to behave?
If you are the one and only person who will, in the history of the project, ever look at or modify the source code and you have a perfect memory, well, then yeah. No need for comments or other programming conventions. Just cowboy it.
Otherwise, use of conventions to (re)enforce intention is not a bad idea.
Upvotes: 0
Reputation: 32525
You're right, there isn't a performance gain. However, there are key differences here:
If you're intention is to create a class where you don't try to modify _bar it really should be a readonly field. There are 2 things I can see here:
Now, it should NOT be readonly if you plan to keep it null until another method is called to initialize it. But, for that I'd look at Lazy<T>
.
Essentially, it's your call. If you truly are going to be the ONLY one modifying this class I'd still say follow those rules. It's the best-practice way of doing it. :)
Upvotes: 2
Reputation: 86509
At the time you wrote this class, you know how you want it to behave.
But:
Your declaration allows the compiler to verify that your intentions are satisfied, cheaply and repeatedly. And it communicates to you and others what your intentions were.
Upvotes: 4
Reputation: 101614
Readonly is good if you want to assign it on instantiation, but don't want it to be alterable. A good scenario I can think of is perhaps a database class with a username/password.
public MyClass(string user, string password){
this.username = username;
this.password = password;
this.connect();
}
With the assumption that the connection is kept throughout the duration the object is alive, it may make sense to store this information for reference but prevent future alterations once the connection is established.
Upvotes: 1
Reputation: 888205
It's always nice to have the compiler prevent you from being dumb.
It's especially nice to have the compiler prevent other people from being dumb, even if they're not familiar with the codebase.
It also serves to tell other people reading your code that the field will never change
Upvotes: 19