Generic Delegates and Lambda Expressions

19 01 2008

.NET 3.5 now provides Generic Delegates which can be used in place of Anonymous Methods

The Generic delegates always encapsulate a method which can accept 1 or more inputs and return a value. So, currently we have,

1) Func<TResult>

2) Func<T,TResult>

3) Func<T1,T2,TResult>

4) Func<T1,T2,T3,TResult>

5) Func<T1,T2,T3,T4,TResult>

Well enough to cater our needs πŸ˜‰

Lets take an example

Problem: Given an array of words, and a desired length, return me all the words which satisfy that length

Let me give the solution straight away,


As you can see we have used the Func<T1,T2,TResult> generic delegate where our first parameter is an array of strings and the second parameter is an integer and third parameter which is the return type is ArrayList

If you look closely, we have also used the Lambda operator too in building the function πŸ˜‰

Now, let us fire our Reflector and see what is there

reflector-function-delegates.jpg this has been transformed into an Anonymous Method and the delegate is nothing but our Func<T1,T2,TResult>

Lets again take a look at how Func<T1,T2,TResult> is declared,

public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

Well, I think that explains it πŸ˜‰

So, what about directly using Lambda expressions..mmm…like this,


That does reduce our lines of code and the way we interpret things πŸ˜€ . I feel the above method is lot more easier than our generic function delegates. So what does our Reflector say about this,


So what is that CS$<>9__CachedAnonymousMethodDelegate2

My Reflector says it is nothing but,


private static
     Func<string, bool> CS$<>9__CachedAnonymousMethodDelegate2;

So, again we are back to our generic delegates 8)

Yes, so as we can see it transforms the lambda expressions into delegates πŸ˜€

From MSDN,

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

I think that explains our Reflector was indeed correct 8)

.NET Source Code (not all, but few) released!

18 01 2008


Yes, as promised earlier, here comes the .NET Source Code πŸ™‚

Read here on how to enable source code debugging from the person who made this possible πŸ˜€

And for a screencast, Daniel Moth has an excellent screencast. Watch it hereΒ 

One notable change is in the licensing of the source code which can be read here at Scott Gu’s blog

Here is the excerptΒ  from the above link,

The .NET Framework source is being released under a read-only reference license. When we announced that we were releasing the source back in October, some people had concerns about the potential impact of their viewing the source. To help clarify and address these concerns, we made a small change to the license to specifically call out that the license does not apply to users developing software for a non-Windows platform that has β€œthe same or substantially the same features or functionality” as the .NET Framework. If the software you are developing is for Windows platforms, you can look at the code, even if that software has “the same or substantially the same features or functionality” as the .NET Framework.


SystemIcons.Shield Icon

18 12 2007

Daniel Moth has blogged about the new SystemIcons.Shield icon added to the set of System.Drawing.SystemIcons collection. This new Shield icon can be used to display whenever an admin task is required to be carried out. You would be familiar with this icon if you are using Vista. For example – If you open the Indexing Options dialog box in Vista,


You can see the Advanced button has the Shield icon in it which indicates that it would be an admin task.

You can refer to Daniel’s post on how to add the Shield icon to Windows Forms application

If you want to add to an Image control in a WPF application, all you have to do is set the Image‘s Source property to a BitmapSource. You can create the BitmapSource object from an Icon as shown below πŸ™‚

BitmapSource source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon
     Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

this.MyImage.Source = source;

And you can see something similar,


Using ExtendGlass

16 12 2007


Few months back I wrote about my small library which helps to enable Glass effects in your WPF applications. Today lets see how we could really make use of it in order to get displays like – a portion of your window has glass effects and the rest dont. Best example to take is Windows Media Player


You should be aware that the WMP has the glass effects enabled at the bottom as seen in the above screenshot.

So how does our sample application going to look like? Below is the screenshot πŸ™‚


Not so bad, but we did achieve to have the upper portion without glass and rest with glass πŸ˜‰

So, what needs to be done to get something like this? Very simple,

1) Decide the margin M for which you are going to enable glass
2) Clip the part of the window W which you don’t want to enable glass to that margin M
3) Give a good background to that part of the Window W

In my above sample, below is the code where I enable the glass interface,

private void Window_SourceInitialized(object sender,EventArgs e)


      Thickness margin = new Thickness();

      margin.Top = 0;

      margin.Left = 20;

      margin.Right = 20;

      margin.Bottom = StackPanelTop.Width;

      ExtendGlassFrame extendGlass =

                new ExtendGlassFrame();

      extendGlass.Extend(this, margin);


Notice that I have set my bottom margin to my StackPanel’s width property. Its just some funky logic placed there πŸ˜‰

And the XAML looks like,

<StackPanel Name="StackPanelTop" Margin="20,0,20,50" Grid.Row="0"
        Background="Lavender" Height="100" Orientation="Horizontal" >
        <Button  Content="No Glass" Background="AliceBlue" Width="80" Height="25"
                HorizontalAlignment="Center"  Margin="9,0,0,0"/>
        <Button  Content="No Glass" Background="AliceBlue" Width="80" Height="25"
                HorizontalAlignment="Center" Margin="10,0,0,0"  />
 <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
       <Button  Content="Transparent!" Background="Transparent"
               Width="80" Height="25"/>
       <Button  Content="Transparent!" Background="Transparent"
               Width="80" Height="25"   Margin="10,0,0,0"/>

So, its some funky logic used here, but I wanted to show that having some logic for your User Interface, you could easily create wonders πŸ˜‰

Multitargeting in Visual Studio 2008

27 11 2007


Many would be now familiar with the word multitargeting in Visual Studio 2008. So, what is it ? πŸ™‚

Prior to Visual Studio 2008, Visual Studio 2003 and Visual Studio 2005 worked only for specific .NET versions and Visual Studio 2005 had .NET 3.0 extensions for .NET 3.0

Things have been made easy for developers in Visual Studio 2008, as now you can work with .NET 2.0, 3.0 and 3.5 projects in the same IDE πŸ˜‰ . This means that you don’t need Visual Studio 2005 anymore! πŸ˜€

Here is a screenshot of options what the New Project menu has in Visual Studio 2008,


Note that for each version of .NET you choose, the project templates gets changed accordingly!

Don’t miss this screencast on Multitargeting in Visual Studio 2008 by Daniel Moth πŸ™‚

Neat Ctrl+Tab replacement in Visual Studio 2008

26 11 2007


If you want to navigate to different tabs(files) opened or previous tab opened inside Visual Studio, we use Ctrl+Tab, similar to Alt+Tab from our Windows Desktop.

This feature has been refined and made to look really good in Visual Studio 2008 πŸ˜€

It now shows previews too πŸ˜‰

Below is a screenshot,


Make Expression Blend work with Visual Studio 2008

26 11 2007

vs08_v_rgb_web.jpg expression_blend.jpg

As I reinstalled my system, I had to reinstall Microsoft Expression Blend too. As expected it didn’t save all the projects in Visual Studio 2008 format and had to use the same old BlendConfigurator tool to configure for Visual Studio 2008. The tool was built for Expression Blend to work with “Orcas” releases and it works for RTMs too πŸ˜‰


After discussing this with Daniel, it is advised to install Expression Blend Service Pack 1 and not to use BlendConfigurator as mentioned above in my post πŸ™‚

Get to know more about Visual Studio 2008

24 11 2007


When I started to interact with my friends about Visual Studio 2008, there were several questions raised by them, like,

1) Will it work in Vista ?

2) Is it not better to wait for a Vista patch like Visual Studio 2005 Service Pack 1 ?

3) Well, do you really think it will support .NET 2.0 and 3.0 ?

4) Will my Visual Studio 2005 solutions work in Visual Studio 2008 ?

I have answers for these but it would be better to read through Daniel Moth’s blog for more information about Visual Studio 2008 and particularly these

I would also highly recommend to download the Visual Studio 2008 and .NET 3.5 Training Kit by Microsoft

My Development Environment

22 11 2007

winvista-button_rgb.jpg vs08_v_rgb_web.jpg ms_net_rgb_web.jpg
After a long time, I re-installed Vista in my laptop and did some changes to partition. The re-install was due to the lack of fair amount of space in my Windows partition. Being a developer, you need to have good amount of free space in the Windows partition to work on. With the release of Visual Studio 2008 and also for going to be released soon Vista SP1, I thought this is the right time to make my machine ready πŸ˜‰

So – What have I installed now ?

Read the rest of this entry »

LINQ to SQL Beta 2 to RTM Changes

22 11 2007


I was totally shocked to see when my solution which I had built using Orcas Beta 2 threw errors when compiled in VS 2008 RTM!

Exploring a bit, I found that the errors generated were related to my LINQ-to-SQL classes.

Read the rest of this entry »