Wednesday, June 25, 2008

C# Puzzle No.9 (beginner)

Consider a delegate declaration which takes a function and returns another function:

delegate Func<int, int> FuncConversionDelegate( Func<int, int> Func );

Your goal is to write a lambda expression defining a converter which would negate values returned by original method:



FuncConversionDelegate Negation = 
    /* 
       replace this with a lambda expression 
       which takes function f and returns function g 
       so that g(x) = -f(x)
    */;

Specifically, you have to replace the commented expression above to a correct definition.


To test the converter, take the identity function:



static Func<int, int> Identity = x => x;

and apply Negation to it:



Negation( Identity )( 5 )
expecting the value of -5 to be returned.

Tuesday, June 24, 2008

C# Puzzle No.8 (beginner)

It is interesting to note what dilemmas the compiler has to actually resolve during the compilation. In the code below there are two possibilities : the compiler will either pick the method from the base class having the same signature or the method from actual class for which the conversion of the parameter is required.

class A
  {
      public void Foo( int n ) 
      {
          Console.WriteLine( "A::Foo" );
      }
  }
 
  class B : A
  {
      /* note that A::Foo and B::Foo are not related at all */
      public void Foo( double n ) 
      {
          Console.WriteLine( "B::Foo" );
      }
  }
 
 
  static void Main( string[] args )
  {
      B b = new B();
      /* which Foo is chosen? */
      b.Foo( 5 );
  }
Could you explain the compiler behaviour without running the code?

Friday, June 20, 2008

C# Puzzle No.7 (intermediate)

After two previous puzzles, you'll surely have no problems to determine the output of following code snippet without actually running it. Specifically, how many times the X will be printed?

List<int> list = new List<int>() { 1, 2, 3 };
 
list.GroupBy ( i => { Console.Write( "X" ); return i; } );
list.ToLookup( i => { Console.Write( "X" ); return i; } );
This Puzzle will let you think about differences (if any) between two seemingly identical methods, GroupBy and ToLookup.

C# Puzzle No.6 (intermediate)

If you have correctly solved the 5th C# puzzle, you should have no problems in predicting the output of following code snippet (without actually running it):

List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
list.Where( i => { Console.WriteLine( i ); return i < 5; } );

Thursday, June 19, 2008

C# Puzzle No.5 (intermediate)

Can you predict the output of the code above without actually running it?

List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
list.FindAll( i => { Console.WriteLine( i ); return i < 5; } );

C# Puzzle No.4 (intermediate)

It is common to use the lambda expression syntax. Whenever a method is required expecting a parameter and returning something, you can provide

parameter => expression

instead of boring



ReturnType Foo( ParameterType parameter )
{
    expression;
}

and then compiler will interfere types for you for both parameter and expression.


The question is, how do you provide lambda expressions for delegates which expects no parameters?



delegate int FooDelegate();
static void Foo( FooDelegate Delegate )
{
    Console.WriteLine( Delegate() );
}
 
static void TheProblem()
{
   Foo( * );
}

Your goal is to fill the * above with the lambda expression equivalent to following delegate:



int ConvertThisToLambdaExpression()
{
  return 5;
}

C# Lambda Expressions Can Contain Method Bodies

I know a lot of people who love and use the lambda expression syntax provided in C# 3.0 as a shortcut for explicit delegate definitions.

Suprisingly however, quite few of them actually know that lambda expression can not only have a suprisingly variable => expression form like:

x => x < 5

but can also contain method bodies as expressions like



x => { Console.WriteLine( x ); return x < 5; }