An Example of Generics Using C# in ASP.Net

Introduction


Developers like you are familiar with object-oriented programming and understand the benefits it offers. One of the big benefits of object-oriented programming is code re-use, in which you create a base class and inherit it in a derived class. The derived class can simply override virtual methods or add some new methods to customize the behavior of the base class to meet yours need. The Generics is another mechanism offered by the Common Language Runtime (CLR) and programming languages that provide one more form of code re-use and algorithm re-use.

Generics provide a code template for creating type-safe code without referring to specific data types. Generics allow you to realize type safety at compile time. They allow you to create a data structure without committing to a specific data type. When the data structure is used, however, the compiler ensures that the types used with it are consistent for type safety. I want to discuss two benefits of generics, one is type safety and another is performance before explaining with an example.

Type Safety


Generics are mostly used for collections but the framework class library also has non-generic collection classes like ArrayList, Hashtable, SortedList, Stack, Queue. So first of all we see an example of a non-generic collection class, in other words ArrayList where we add integer values to the array list and perform addition operation on the list values.

You need to create a class, ArrayListOperation, and define an Addition method as in the following code snippet.
  1. using System.Collections;  
  2. namespace TypeSafety  
  3. {  
  4.     class ArrayListOperation  
  5.     {  
  6.         public static int Addition()  
  7.         {  
  8.             ArrayList list = new ArrayList();  
  9.             list.Add(5);  
  10.             list.Add(9);  
  11.             int result = 0;  
  12.             foreach (int value in list)  
  13.             {  
  14.                 result += value;  
  15.             }  
  16.             return result;  
  17.         }  
  18.     }  
  19. }  
When you run this code you get a return value from the method.

As you saw in the previous example there is an array list of integer values and get the result as expected but now add one more value to the ArrayList that the data type is float and perform the same Addition operation. Let's see the updated code in the following snippet.
  1. using System.Collections;  
  2. namespace TypeSafety  
  3. {  
  4.     class ArrayListOperation  
  5.     {  
  6.         public static int Addition()  
  7.         {  
  8.             ArrayList list = new ArrayList();  
  9.             list.Add(5);  
  10.             list.Add(9);  
  11.             list.Add(5.10);  
  12.             int result = 0;  
  13.             foreach (int value in list)  
  14.             {  
  15.                 result += value;  
  16.             }  
  17.             return result;  
  18.         }  
  19.     }  
  20. }  
In the code above, all three values are easily added to the array list because the ArrayList class Add() method values are an object type but when retrieving the values using each statement each value will be assigned an int data type variable but this array list has a combination of both integer and float type value and float values won't cast to an int implicitly. That is why the code will give the exception “Specified cast is not valid.” That means that an ArrayList is not type safe. What this means is that an ArrayList can be assigned a value of any type.

Generics allow you to realize type safety at compile time. They allow you to create a data structure without committing to a specific data type. When the data structure is used, however, the compiler ensures that the types used with it are consistent for type safety. Generics provide type safety, but without any loss of performance or code bloat. The System.Collections.Generics namespace contains the generics collections. Now let's see an example with a generic collection List.
  1. using System.Collections.Generic;  
  2. namespace TypeSafety  
  3. {  
  4.     class ListOperation  
  5.     {  
  6.         public static int Addition()  
  7.         {  
  8.             List<int> list = new List<int>();  
  9.             list.Add(5);  
  10.             list.Add(9);  
  11.             int result = 0;  
  12.             foreach (int value in list)  
  13.             {  
  14.                 result += value;  
  15.             }  
  16.             return result;  
  17.         }  
  18.     }  
  19. }  
In the code above we define a list int type, in other words we can only add an integer value to the list and can't add a float value to it so when we retrieve values from the list using a foreach statement we only get an int value. When you run this code you will get a return value from the method. Now you can say that a generic collection is type safe.

Performance


Before discussing performance we need to understand object data types in C#, so what are object data types? From the MSDN “The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of the type object. When a variable of a value type is converted to an object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed.” I hope it make sense.

Before generics, the way to define a generalized algorithm was to define its entire member to the Object data type. If you wanted to use the algorithm with value type instances, the CLR needed to box the value type instance prior to calling the members of the algorithm. This boxing causes memory allocations on the managed heap, that causes more frequent garbage collections, which, in turn, hurt an application's performance.

Since a generic algorithm can now be created to work with a specific value type, the instances of the value type can be passed by value, and the CLR no longer must do any boxing. In addition, since casts are not necessary, the CLR doesn't need to check the type safety of the attempted cast, and this results in faster code too.

Now let's see the syntax of the Add() method for non-generic and generic collection classes. We are discussing two collection classes, one for non-generic (ArrayList) and another is generic (List).

the syntax of the Add() method of ArrayList: 
  1. public virtual int Add(object value);  
The preceding line of method signature represents that each value to be added to an ArrayList will be an object type, in other words if you are using a value type to be added to an ArrayList then it will be cast yo an object type before being added to ArrayList.

The syntax of the Add() method of List<T>:
  1. public void Add(T item);  
The preceding line of method signature represents that there is no need to do a cast to add an item to a generic type list. If the List type is T then the item will be T type.

Now you understand who has better performance. A generic collection class has better performance than a non-generic collection class. Let's see an example in the following code snippet.
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Diagnostics;  
  5. namespace Performance  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             NonGenericPerformance();  
  12.             GenericPerformance();  
  13.             Console.ReadKey();  
  14.         }  
  15.         static void NonGenericPerformance()  
  16.         {  
  17.             long operationTime = 0;  
  18.             ArrayList arraylist = new ArrayList();  
  19.             Stopwatch sw = new Stopwatch();  
  20.             sw.Start();  
  21.             for (int i = 1; i <= 100000; i++)  
  22.             {  
  23.                 arraylist.Add(i);  
  24.             }  
  25.             operationTime = sw.ElapsedMilliseconds;  
  26.             Console.WriteLine("Array List {0} values add time is {1} milliseconds", arraylist.Count, operationTime);  
  27.             sw.Restart();  
  28.             foreach (int i in arraylist)  
  29.             {  
  30.                 int value = i;  
  31.             }  
  32.             operationTime = sw.ElapsedMilliseconds;  
  33.             Console.WriteLine("Array List {0} values retrieve time is {1} milliseconds", arraylist.Count, operationTime);  
  34.         }  
  35.         static void GenericPerformance()  
  36.         {  
  37.             long operationTime = 0;  
  38.             List<int> list = new List<int>();  
  39.             Stopwatch sw = new Stopwatch();  
  40.             sw.Start();  
  41.             for (int i = 1; i <= 100000; i++)  
  42.             {  
  43.                 list.Add(i);  
  44.             }  
  45.             operationTime = sw.ElapsedMilliseconds;  
  46.             Console.WriteLine("List {0} values add time is {1} milliseconds", list.Count, operationTime);  
  47.             sw.Restart();  
  48.             foreach (int i in list)  
  49.             {  
  50.                 int value = i;  
  51.             }  
  52.             operationTime = sw.ElapsedMilliseconds;  
  53.             Console.WriteLine("List {0} values retrieve time is {1} milliseconds", list.Count, operationTime);  
  54.         }  
  55.     }  
  56. }  
Let's run the code above. The results are as in Figure 1.1.



Figure 1.1 Performance between generic and non generic collection.

How to reuse code using Generics?


Here I will introduce a simple concept of code reuse using generics. I will populate UI lists (Dropdown, Checkbox, RadioButton) using a common method. So let's see the code.

First of all create a table Master that has the three fields Id, Title and Type (UI entity type). Let's see the database script for table creation and database insertion.
  1. Create Table Master  
  2. (  
  3. Id int identity(1,1) primary key,  
  4. Title nvarchar(50) not null,  
  5. Type int not null  
  6. )  
  7. Insert Into Master values ('Jaipur',1),('Jhunjhunu',1),  
  8. ('Cricket',2),('Football',2),('Male',3),('Female',3)  
Now create an enum for the types on the UI as per our form design requirements.
  1. namespace GenericUIList  
  2. {  
  3.     public enum Types  
  4.     {  
  5.         NativePlace = 1,  
  6.         Hobby = 2,  
  7.         Gender = 3  
  8.     }  
  9. }  
Here the enum attribute values are the same as the Type field values in the database table Master.

Write the connection string in code behind, like:
  1. <connectionStrings>     
  2.     <add name ="genericConn" connectionString="Data Source=SANDEEPSS-PC;database=Development;user=sa;password=******"/>  
  3. </connectionStrings>  

Now design a form that has three lists for NativePlace (Dropdownlist), Hobbies (Checkbox list) and Gender (Radio button list) as in the following code snippet.

  1. <p>Native Place : <asp:DropDownList ID="ddlNativePlace" runat="server"></asp:DropDownList></p>  
  2. <p>Hobbies : <asp:CheckBoxList ID="chkHobbies" runat="server"></asp:CheckBoxList></p>  
  3. <p>Gender : <asp:RadioButtonList ID="rbGender" runat="server"></asp:RadioButtonList></p>  
Now we create a generic method in the code behind file and call that method on page load. Let's see the following code snippet on the .cs page of the web form.

  1. using System;  
  2. using System.Web.UI;  
  3. using System.Web.UI.WebControls;  
  4. using System.Configuration;  
  5. using System.Data.SqlClient;  
  6. using System.Data;  
  7. namespace GenericUIList  
  8. {  
  9.     public partial class _Default : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.             if (!Page.IsPostBack)  
  14.             {  
  15.                 PopulateUIList<DropDownList>(ddlNativePlace, Types.NativePlace);  
  16.                 PopulateUIList<CheckBoxList>(chkHobbies, Types.Hobby);  
  17.                 PopulateUIList<RadioButtonList>(rbGender, Types.Gender);  
  18.             }  
  19.         }  
  20.         public void PopulateUIList<T>(T list, Types type) where T : ListControl  
  21.          {  
  22.              string connectionString = ConfigurationManager.ConnectionStrings["genericConn"].ConnectionString;  
  23.              using (SqlConnection con = new SqlConnection(connectionString))  
  24.              {  
  25.                  if(con.State == ConnectionState.Closed)  
  26.                  {  
  27.                      con.Open();  
  28.                  }  
  29.                  string cmdText = "Select Id,Title from Master Where Type = @type";  
  30.                  using (SqlCommand cmd = new SqlCommand(cmdText, con))  
  31.                  {  
  32.                      cmd.Parameters.Add(new SqlParameter("@type",(int)type));  
  33.                      DataTable dt = new DataTable();  
  34.                      IDataReader dr = cmd.ExecuteReader();  
  35.                      dt.Load(dr);  
  36.                      list.DataSource = dt;  
  37.                      list.DataTextField = "Title";  
  38.                      list.DataValueField = "Id";  
  39.                      list.SelectedIndex = 0;  
  40.                      list.DataBind();  
  41.                  }  
  42.              }  
  43.          }  
  44.     }  
  45. }  

Run the application and get the results on the page as in Figure 1.2.



Figure 1.2 Populate UI List

Conclusion


I hope you now understand how generics are useful over non-generics in this article. We have also explained how to reuse code using generics. If you have any doubt then you can comment here and directly connect to me by https://twitter.com/ss_shekhawat.


Similar Articles