LINQ (Language Integrated Query) - Part 3

This is third part of the ‘LINQ’ series posts that I have started from here. And in this post, you will learn something on ‘Generic’ like what are generic types, why we need it in LINQ?



Previous Posts:

First of all let’s understand what are ‘Arrays’, what are ‘.NET Collections’ and what are ‘.NET Generic Collections’. You will see following advantages and disadvantages in examples given below.

Collection Types
Advantages
Disadvantages
Arrays
Strongly Typed (Type Safe)
  • Zero Index Based
  • Cannot grow the size once initialized
.NET Collections (like ArrayList, Hashtable etc)
  • Can grow in size
  • Convenient to work with Add, Remove
Not Strongly Typed
.NET Generic Collections (like List<T>,  GenericList<T> etc)
  • Type Safe like arrays
  • Can grow automatically like ArrayList
  • Convenient to work with Add, Remove


Now, let’s talk on each of the collection type given in above table to feel real pain of developments.

Array


If you use array:
  • You can’t enter more values beyond the initialized size otherwise you will see a runtime error saying ‘index was outside the bounds of the array’.
  • If you declare array as an int type then, you are limited to enter integer values only otherwise you will see a compile time error saying ‘cannot implicitly convert type string to int’, all because array is type safe.
  • Array is index based, so you always need to write index number to insert value in collection.
  • You don’t have add or remove methods here.

ArrayList (System.Collection)


If you use ArrayList:
  • You will get auto grow feature, means initial size will not limit you.
  • You don’t option to declare the data type at declaration time on the top, so you can add any value like string, Boolean, int etc. Remember to care it in foreach loop at runtime.
  • You will have some very useful methods like Remove, RemoveAt, RemoveRange etc. You don’t have any option like index.

List<T> (System.Collection.Generic)


Now, in short we can say, list has both features that we have in array or arraylist.

Why we need List (which is generic type) in LINQ?

LINQ queries are based on generic types, which were introduced in version 2.0 of the .NET Framework. LINQ queries returns collection and we don’t even know what type of data we will get from database and that is the reason we use generic type like List<T>, IEnumerable<T>, IQueryable<T> etc. For example:


In above example, we have used IEnumerable<T> interface and LINQ query and then by using foreach loop I’m able to get my data.

One more example:

IEnumerable<Customer> customerQuery =
    from cust in customers
    where cust.City == "Bokaro" 
    select cust;

foreach (Customer customer in customerQuery)
{
    Console.WriteLine(customer.LastName + " = " + customer.Age);
}

In above example you can see, using LINQ query I’m not only limited to select the string value that is LastName, I’m also able to select integer value that is Age.

So, this could be the great example of generic classes.

Here is some counterpart of Generic Collections over non-generic collections:

Collections
(System.Collection)
Generic Collection
(System.Collection.Generic)
ArrayList
List<T>
Hashtable
Dictionary<Tkey, Tvalue>
Stack
Stack<T>
Queue
Queue<T>

I hope you will find it useful. Thanks for reading.

Comments

Popular posts from this blog