Wednesday, September 28, 2011

Only one

Just another LINQ puzzle.

Let's assume that we have sequence of objects of type Item:

class Item
{
   public int Age{get;set;}
}

How could you return all objects from the sequence that have Age value maximized (in a single LINQ query and single enumeration of the sequence)?

If the question was about returning the first object, the answer is straightforward:

return items.OrderByDescending(x=>x.Age).First();

Tuesday, September 20, 2011

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"]);

Friday, July 15, 2011

Trace the stack

There are two ways to get current stack trace:

1. Using System.Diagnostics.StackTrace:
var stackTrace = new StackTrace(true);
foreach (var r in stackTrace.GetFrames())
{
       Console.WriteLine("Filename: {0} Method: {1} Line: {2} Column: {3}  ",
           r.GetFileName(),r.GetMethod(), r.GetFileLineNumber(),
           r.GetFileColumnNumber() );
}

2. Using System.Environment:

Console.WriteLine(Environment.StackTrace);

Wednesday, June 29, 2011

Coconut

Simple, but little painful snare regarding Streams and StreamWriters.

Let's imagine we have a function that print some data to a TextWriter:

void Print(TextWriter writer)
{
 // ...
}

Also we have a stream (e.g. Http output stream), which we want to use to print in data. So, we'll write something like:

using (Stream stream = GetOutputStream()){
  var streamWriter = new StreamWriter(stream);
  Print(streamWriter);
}

This code is fair enough - as far as StreamWriter uses the disposed stream, there should not be any resource leaks as far as we're disposing the thread. But there is some bug which can lead to data losses.

Monday, April 11, 2011

Ex-tension

There is a simple rule of thumb in which namespace extensions methods should be put. If this is a general purpose extension method - put it in the same namespace as 'this' parameter:

namespace System
{
 public static class StringExtensions
 {
  public static string Fmt(this string format, params object[] args)
  {
   return string.Format(format, args);
  }
 }
}

If an extension method use some specific parameters, but still is more or less general purpose, put in into the narrowest namespace of method parameters.

And if the method is specific method, put it in the namespace where it will be used. Also it is good to mark this method as 'internal'.