Nice reference card for quick revision


Yesterday while browsing encountered the following site

The site has got nice reference material written by experts.

Do check out this site !!

Bye …

Delegate, Anonymous Methods and Lambda Expressions in C#

Delegate acts as a function pointer.

We define the function to which the delegate can point to in this manner

// Delegate which will point to a method which has no parameter and returns nothing

public delegate void MySimpleDelegate();

// Delegate which will point to a method which takes string as parameter and returns string as well

public delegate string MyComplexDelegate(string message);


Now comes our two function to which delegates would be pointing 

public void SimpleDelegate(){

MessageBox.Show(“My Simple Delegate”);


public string ComplexDelegate(string name){


return “Hi “ + name;


// We have created an instance of our MySimpleDelegate

// which would be referencing SimpleDelegate function

MySimpleDelegate myD = SimpleDelegate;

// when we call the delegate it calls the method which it is pointing to


// Same thing with our MyComplexDelegate

MyComplexDelegate myAD = AnotherDelegate;



// If we know that our function SimpleDelegate() and ComplexDelegate(string name)

// wouldn’t be called by any other code other than the delegate itself we can

// make use of anonymous method i.e. method with no name

// Here again we have done the same thing but used anonymous method

// because we aren’t going to use it anywhere else

MySimpleDelegate myD = delegate


MessageBox.Show(“Simple Delegate”);



MyComplexDelegate myAD = delegate(String name)



return “Hi “ + name;



// Now let’s shorten our code further using lambda expression

// () -> no input parameter

// => -> lambda operator

// MessageBox.Show(“Simple delegate”) -> statement block

MySimpleDelegate myD = () => MessageBox.Show(“Simple delegate”);


//(String name) -> explicitly typed parameter

MyComplexDelegate myAD = (String name) => { MessageBox.Show(name); return name; };





Using .NET Assembly (DLL) in ASP page

1. Create a Assembly file using following:
        File >> New >> Project >> Class Library >> Name = MyLibCSharp
2. Add a function HelloWorld. Code will look as follows:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

namespace MyLibCsharp




public class Class1


public String HelloWorld(String name)


return (“Hellow World; Welcome “ + name);




3. Sign the Assembly (To enable it to be used by multiple Applications):

Go to Project >> MyLibCSharp Properties >> Signing >> Sign the Assembly >> New >> Key.snk

4. Enable for COM Interop:

Go to Project >> MyLibCSharp Properties >> Build >> Output >> Check the “Register for COM interop.

5. Build the Assembly.

6. Add to GAC using “Visual Studio Command Prompt” the assembly is located in DEBUG/RELEASE folder.

gacutil -I MyLibCSharp.dll

7. Execute the following:

Regasm /tlb /codebase MyLibCSharp.dll

8. ASP code:


Dim foo

set foo = Server.CreateObject(“MyLibCSharp.Class1”)

Response.Write (foo.HelloWorld(“test”))


9. To unregister the assembly

Regasm /unregister MyLibCSharp.dll

10. And to remove the same from GAC (Global assembly cache)

Go to


find the assembly you want to remove

select it , right click it and select uninstall



Nice website for C# developer

Do check out this very very useful web-site for c# developer for understanding
multi threading and it has got some wonderful tools like

QueryExpression – which has an user interface similar to query analyzer using which we can query oracle, sql server and other databases. It is also very light weight.

LinqPad for practicing the Linq syntax.

Creating custom application exception C#


There are times when we are creating a custom class and the class needs to have it’s own application specific exception which can be thrown so that calling program can be aware of the error condition.

Say we have a class named BillingUpdate which has a condition that the billing amount should never be less than 10,000.

Say it has a function which accepts billing amount in one of it’s methods as a parameter and we need to make the user of this function aware of the condition that it can’t take billing amount less than 10000.

In this case what we can do is

create a custom class BillingException which inherits SystemException class

// creating a custom class that inherits from SystemException.
class BillingException :SystemException
// overloading the constructor for passing the message associated with the exception
public BillingException(string message)
: base(message)

Now to use this exception class we can do the following

public void GetBillingAmount(decimal billingAmount)
if(billingAmount < 10000M)
throw new BillingException(“Billing amount can’t be less than 10000”);

And the calling code can do something like this

BillingUpdate billingUpdate = new BillingUpdate();
BillingException ex)