Friday, August 19, 2011

Not so static

Today I've caught curious exception in the code like below:

class Drawer
{
    private static readonly Pen = new Pen(Color.White) { Width = 5 };

    public Bitmap DrawEllipse(int width, int height)
    {
       var bitmap = new Bitmap(width, height);
       using (var graphics = Graphics.FromImage(bitmap))
       {
         graphics.DrawEllipse(Pen, 0,0, width, height);
       }
       return bitmap;
    } 
}

Just an "optimization" not to create a pen every time and keep it static for "caching".

Friday, August 12, 2011

Is it C#?

Some weird but pretty code trick.

It is c#?
var hash = new Hash
                  {
                   a => "A value", 
                   b => "B value"
                  };
Console.WriteLine(hash["a"]);