Monthly Archives: February 2012

Google Search Confined to Domain

This is one of those posts where I am recording something here which I keep forgetting.
The syntax for doing a Google search, confined to a domain is:

[searched expression] site:[domain]

For example, to search this site for WebformsMVP:

WebformsMVP site:localhost:8090

Piece of cake. Hopefully I will remember this syntax now.

Comparison Delegate – Used to Compare (duh)

The comparison delagate has only one signature:

public delegate int Comparison<in T>(T x,T y)

It is a simple little delegate which follows the same rule as CompareTo (IComparable) and Compare (IComparer):

Logic Result
x < y -1
x == y 0
x > y 1

An example of a method in the framework which takes the Comparison delegate as a parameter is the Sort method of the List class. You can create more than 1 method which conforms to this delegate, thereby giving the caller the power to decide how they want the Sort order to be determined:

        private static int CompareEmployees(Employee firstEmployee, Employee secondEmployee)
        {
            if (firstEmployee == null && secondEmployee == null)
            {
                return 0;
            }

            if (firstEmployee == null)
                return -1;
            if (secondEmployee == null)
                return 1;

            return firstEmployee.Nr.CompareTo(secondEmployee.Nr);
        }

        private static int CompareEmployeesReverse(Employee firstEmployee, Employee secondEmployee)
        {
            if (firstEmployee == null && secondEmployee == null)
            {
                return 0;
            }

            if (firstEmployee == null)
                return 1;
            if (secondEmployee == null)
                return -1;

            if (firstEmployee.Nr.CompareTo(secondEmployee.Nr) < 0)
            {
                return 1;
            }
            else if (firstEmployee.Nr.CompareTo(secondEmployee.Nr) > 0)
            {
                return -1;
            }
            else 
                return 0;
        }

And to use it:

            List<Employee> employees = GetEmployees();

            employees.Sort(CompareEmployees);

There’s not too much more to say. This delegate gets used quite a bit in LINQ.

Get the code:

Func Delegate and Default Values

Another really common and useful delegate is the Func delegate. This delegate has many overloads which all return a result. They differ in the number of parameters which they can be passed:
Func Signatures
One scenario in which I have used this delegate is in a SessionState service which I wrote recently, effectively making SessionState strongly typed. By way of example, I have simplified that idea into a Console application which uses a Dictionary collection instead of HttpContext.Current.Session. First, I created the MemoryBucket class:

    internal class MemoryBucket
    {
        private Dictionary<string, object> items;

        internal MemoryBucket()
        {
            items = new Dictionary<string, object>(StringComparer.Ordinal);   
        }

        internal void SetValue<T>(string key, T value)
        {
            if (!this.items.ContainsKey(key))
            {
                items.Add(key, value);
            }
            else
            {
                items[key] = value;
            }
        }

        internal T GetValue<T>(string key, Func<T> createDefault)
        {
            T retval;

            if (!ReferenceEquals(null, items))
            {
                if (items.ContainsKey(key) && items[key].GetType() == typeof(T))
                {
                    retval = (T)items[key];
                }
                else
                {
                    retval = createDefault.Invoke();
                    items[key] = retval;
                }

                return retval;
            }
            else
            {
                throw new NullReferenceException("There is no Memory object available.");
            }
        }
   }

Looking at the GetValue method, if the value passed into this method is either null, or not the same type as the item which correlates to that key, a default value is returned. That default value is determined by the method (or lambda) which the calling code passes in as the 2nd parameter. So, the delegate gives the power to the calling code to control how the default value is generated. For example:

        static void Main(string[] args)
        {
            MemoryBucket bucket = new MemoryBucket();

            bucket.SetValue("My Name", "dave");
            Console.WriteLine(bucket.GetValue("My Name", () => "David"));

            bucket.SetValue("My Name", 10);
            Console.WriteLine(bucket.GetValue("My Name", () => "David"));

            Console.Write("\nPress any key to kill console...");
            Console.ReadKey();
        }

As you can see, the second time I call GetValue follows a call to SetValue which sets the “My Name” item to 10. As the System.Int32 type is not of the same type as the System.String type, the default value will be returned. That value being the string “David”, which is the return value in the lambda expression.