My Profile

Thursday, February 10, 2011

Generic Collections


In the previous program, we have used ArrayList class which is not preferred with respect to performance. i.e., as the classes under System.Collections undergo Boxing and Unboxing, the performance of the application degrades. So, System.Collections is not much preferred to be used.

In .Net generic classes are provided for better programming.
“System.Collections.Generic” is the namespace under which all the generic classes are provided. One of the important generic class is List<T>. it is the class inside it which is a collection of type T. T is a type like int, string, Employee etc.,

Example:       List<int> obj;
//List<int> is the class inside it which is a collection of type int.
//obj is the variable of that collection class.

Example: In the previous example modify the utility class as following:

Using System.Collections.Generic;
Public class utility
{
            Public static List<string> GetCountries()
            {
                        List<string> obj = new List<string>();
                        obj.Add(“United States”);
                        obj.Add(“India”);
obj.Add(“Australia”);
obj.Add(“United Kingdom”);
                        return obj;
            }
}

That’s it… the program will run perfectly. No need to change anything in the form code.
This is the advantage we’ll get if we maintain code file separately instead of combining code with the form.

Hope you understood how to handle generic classes and data binding.
Check yourself whether you’re clear or not with the following exercise.

“take one List control for Countries and another list control for Players, whenever user selects one country the corresponding players should come in second List control. Now take another list control for features of player, whenever user selects player atleast 5 features of that particular player should come in 3rd List control. And also take an image control display the player picture when the player is selected along with the features.”

---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support" .

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More