Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Default Extension Methods

0.00/5 (No votes)
10 Aug 2011 1  
Discussion about default extension method

In this post, I am going to discuss about Default extension method(s) which are useful when we query the data form the collection, array or when performing operation while coding using linq to SQL.

In the .NET Framework, there are number of extension methods which allows perform function on the collection. But the problem arises when we don't know the collection or array empty or there is no element after we apply filter.

Methods like .First<t>(), .Last<t>(), .Single<t>() always throws exception InvalidOperationException when object collection is empty or if there is no element available after applying filter condition.

List<int> lstInt = new List<int>(new int[] {15,11,20,30});
var data = lstInt.Single(i => i < 10);

Default Methods

To avoid the exception, .NET Framework has default methods like .FirstOrDefault<t>(), .LastOrDefault<t>(), .SingleOrDefault<t>(), which return the default value if there is no element in the collection or array is empty.

List<int> lstInt = new List<int>(new int[] {15,11,20,30});
var data = lstInt.SingleOrDefualt(i => i < 10);
if (data == 0)
{
   Console.WriteLine("No element found");
}

What if I have collection which is empty and apply the default method, it will return Null value.

class Employee
{
        public string Name { get; set; }
        public int Age { get; set; }

        public static void GetEmployee()
        {
            List<Employee> lstEmp = new List<Employee>();
            var lst = lstEmp.SingleOrDefault();
            if (lst == null)
              Console.WriteLine("list is empty");
        }
}

But I want to display the default value if collection is empty. So for the solution .NET Framework provided method DefaultIfEmpty() which allows to do so.

class Employee
{
        public string Name { get; set; }
        public int Age { get; set; }

        public static void GetEmployee()
        {
            List<Employee> lstEmp = new List<Employee();
            Employee defualtEmp = new Employee() { Name = "defualt", Age = 0 };
            var lst = lstEmp.DefaultIfEmpty(defualtEmp).Single();
            
            Console.WriteLine("Employee :" + lst.Name);
        }
}

The above method is also helpful when I am apply filter on the collection and collection is empty.

public static void GetEmployee()
{
            List<Employee> lstEmp = new List<Employee>{
                                new Employee(){Name = "Pranay",  Age =25 },
                                new Employee(){Name = "Krunal", Age = 26},
                                new Employee(){Name = "Hanika", Age = 26} 
            };

            Employee defualtEmp = new Employee() { Name = "defualt", Age = 0 };

            var emp = lstEmp.Where(e=>e.Age<20).DefaultIfEmpty(defualtEmp);
            foreach(Employee e in emp)
            Console.WriteLine("Fond Employee: " + e.Name);
}

The above code is the example but it's very helpful when I am binding collection with the list controls.

Summary

The default methods play an important role when we code with the collection or with the linq to SQL or linq to XML and we don't know whether the collection is empty or not. You can get detailed information about the methods on MSDN.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here