Monday, April 11, 2011

Action vs Func vs Predicate delegates

If you are working with .Net 4.0, you must have come across tailored-made delegates like Action, Func or Predicate many times. Today I shall discuss about them and explain how they can be useful in your daily coding practices.
Action delegate: Action delegate is an inbuilt delegate who can encapsulate any function that takes any type as input and returns nothing (void). If you see the metadata, Action delegate is nothing but

  public delegate void Action<in T>(T obj);

Example: Let say I want to encapsulate a function which prints the string input on console and which has return type void.

Approach 1:

 class Program
    {
        public delegate void myActionDel(string s);

        static void Main(string[] args)
        {
            myActionDel del = WriteToConsole;
            del("delegate works");
            Console.Read();
        }

        public static void WriteToConsole(string s)
        {
            Console.WriteLine(s);
        }
    }

What I am doing here is defining a delegate which can takes a string input and returns void. I am wrappping a function "WriteToConsole" of same signature as my delegate and invoking the delegate.


Approach 2:

class Program
    {

        static void Main(string[] args)
        {
            Action<string> myActionDel = WriteToConsole;
            myActionDel("delegate works");
            Console.Read();
        }

        public static void WriteToConsole(string s)
        {
            Console.WriteLine(s);
        }
    }

you can see here that I did not need to define a delegate explicitly since I knew that my Action delegate is a delegate which fulfills my purpose, it takes a single input parameter and does not return anything.

Examples from .NET framework:
1. Foreach in list type takes Action delegate as input. If you look the metadata of Foreach in list type, it will look like as following

public void ForEach(Action action);

The following code will print "Abhishek" three times (on each invocation of delegate). You can also check below in how many different ways I can use the delegate.

                                  List<string> names = new List<string>();
            names.Add("Abhishek");

            //anonymous delegate way
            names.ForEach(delegate(string s) { Console.WriteLine(s); });

            //lambda expression way
            names.ForEach(s => Console.WriteLine(s));

            //proper delegate way
            Action<string> del = (string s) => { Console.WriteLine(s); };
            names.ForEach(del);



Func Delegate: Now let’s discuss about Func delegate. This is the most used inbuilt delegate since it can take any number of parameters (though there is a cap but that is too high for our general purposes) and it can return a type. Looking at metadata for Func delegate which can encompass a function taking two input parameters and a return type looks something like below

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

Kindly note that when you define a Func delegate, the last parameter is always a return type.

Example:

class Program

    {

        static void Main(string[] args)

        {

            Func<stringstring> myFuncDel = WriteToConsole;

            Console.WriteLine(myFuncDel("This is an example of Func delegate"));

            Console.Read();

        }



        public static string WriteToConsole(string s)

        {

            return s;

        }

    }

Note that Func tells that this can encapsulate a function which takes a string input parameter and returns a string output.


Predicate: Predicate is a delegate which takes any object type as input and returns a Boolean. A predicate is equivalent to

 public delegate bool Predicate<in T>(T obj);

or

public Func<objectbool> myPredicate;

Predicates are designed to be used in places where you want to evaluate something and return a Boolean value based on whether your test passed or failed.

 class Program

    {

        static void Main(string[] args)

        {

            Predicate<string> MyPredicate = CheckMyName;

            Console.WriteLine("Is my name Abhishek: " + MyPredicate("Abhishek"));

            Console.Read();

        }



        public static bool CheckMyName(string s)

        {

            return s == "Abhishek";

        }

    }

In .NET framework, you will find predicates used in for e.g. List.Find, Array.Find etc.

public T Find(Predicate match);



Wednesday, August 18, 2010

Best way to end ASP.NET session

There are mainly three methods in Session object which serve similar purpose.

1. Session.Clear(): This will immediately remove all objects stored inside Session object. Session object basically stores a collection of key(string) vs value(object). After calling this, you will find Session.count equals to zero but Session object itself will still be alive and you can again add new key/value items to it.

2. Session.RemoveAll(): It behaves exactly the same way as Session.Clear. It just wraps Session.RemoveAt at one shot. You can specify index in Session.RemoveAt to remove items one by one. You can also call Session.Remove and pass key to remove that key/value pair item. It could be slower than Session.Clear which essentially is made just to clear all the items.

3. Session.Abandon(): If you have used Session.Clear or Session.RemoveAll, though you have got rid of any and all objects stored in session but session object itself remains alive and could be used further. Session.Abandon just cancels the current session and session object gets destroyed.
The only point to remember here is that if you have used Session.Abandon, it will just mark the session to be destroyed but nothing will happen to session object for that request. As soon as the current request gets completed, session object will be canceled. So don't get surprised if nothing looks to happening to session object just after calling the abandon method, in the next request, you will find that session object to be destroyed and a new one to be created.

Take your web application offline without any change in web server or application

This might come handy when you are in maintenance mode of your application and you want to let your user know it. How would you do it without shutting down your application or without writing additional code to redirect all requests?
The only thing you have to do is to create a file with name app_offline.htm, decorate it however you want and just put it inside the root directory of your application. Rest everything will be taken care of .NET handlers. All users visiting to your site will automatically see your offline page.
Whenever you want your application to be up, just remove that file and that's it.