Comments:
Posted by
Mikael Lundin
@
26 May 2009
12:30
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
14:05
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
01:44
lol - nice sample. Maybe 'guaranteed' wasn't the perfect choice of word.
Posted by
Doron Assayas
@
31 May 2009
14:19
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
09:48
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
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
08:10
I agree, re: style/taste...
Just couldn't resist, I like my style the most! :0
Great blog content, btw.