Skip Navigation LinksHome > View Post

My favourite underused keyword in C# is...

readonly.

Why this little chap isn't used more I don't know.

He's pretty simple. Any field static or otherwise can be marked readonly which means that it can only be written to in the constructor or an in-line initializer.

public class Foo
{
    private readonly static Bar _bar;
    private readonly static object _padlock = new object();
    private readonly object _wee;
    
    static Foo()
    {
        _bar = new Bar();
    }

    public Foo(Object wee)
    {
        _wee = wee;
    }
}

It's common that we'll store types passed on the constructor in a private field and our intention is that these instances won't change for the lifetime of the class (or in case of statics, for the lifetime of the appdomain). So the rule of readonly is simple:

If your intention is that field should be set once and once only; mark it readonly and this is guaranteed.

What's your favourite underused keyword in C#?

Tags: C#

 
Josh Post By Josh Twist
8:18 AM
26 May 2009

» Next Post: My new favourite keyboard shortcut in Visual Studio
« Previous Post: Measuring the x,y distance between two elements in Silverlight

Comments are closed for this post.

Posted by Mikael Lundin @ 26 May 2009 12:30 PM
Yield is a keyword that I forget from time to time. It can do a lot to performance if used correctly. :)

Posted by mihailik @ 26 May 2009 2:05 PM
Actually, readonly gives pretty lax guarantees. A readonly field may be changed multiple times, and at very unpredictable places. They have to do some gymnastics, but nevertheless there are legal ways (and I'm not talking about Reflection).

partial class Immutable
{
public readonly string Text;
public Immutable(string text) { this.Text = text; }
}

void DoSomething(Immutable i)
{
// you wouldn't expect this to print different numbers, would you?
while(true)
{
Console.WriteLine(i.Text);
}
}

partial class Immutable
{
public Immutable()
{
Thread th = new Thread(delegate()
{
// oh, it will be changed right under your feet
DoSomething(this);
});
th.Start();
while(true)
{
this.Text = Guid.NewGuid().ToString();
}
}
}

Posted by josh @ 27 May 2009 1:44 AM
lol - nice sample. Maybe 'guaranteed' wasn't the perfect choice of word.

Posted by Doron Assayas @ 31 May 2009 2:19 PM
I use 'public readonly' a lot when I'm writing quick helper classes. It saves me the trouble of defining a private field and a public get accessor, which seems silly to me anyhow but which I follow because of convention.

Posted by Srdjan @ 06 Jun 2009 9:48 AM
how about a favorite overused keyword?

private

Unnecessary most of the time, including in the sample code
in your post.... :)

Posted by josh @ 06 Jun 2009 11:52 AM
Maybe, personally I prefer the private - I like to be as explicit about most things as possible. But that's just a question of style IMO - more of a religious argument than a technical one.

:)

Posted by Srdjan @ 07 Jun 2009 8:10 AM
I agree, re: style/taste...
Just couldn't resist, I like my style the most! :0

Great blog content, btw.

© 2005 - 2014 Josh Twist - All Rights Reserved.