Click here to Skip to main content
11,433,997 members (58,451 online)
Click here to Skip to main content

Sample ASP.Net Application for Generics

, 12 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple Application in ASP.Net to demonstrates the use of Generics

Introduction

Generics are a new feature in version 2.0 of the C# language and the common language runtime (CLR)

When you use generics, you are creating classes or methods that use a generic type, rather than a specific type. For example, rather than creating a type-specific, you could create a reusable List class using generics.

How is that different from the ArrayList class?

The System.Collection.ArrayList can be used with any objectn, but no type checking is done when objects are passed to methods. You have to manually cast objects back to our type when retrieving; which makes the code harder.

Using the code

I have a Strongly Typed Class named "StudentList" and Generics Class named "MyCustomList<T>" and a sample class named "Student".

"StudentList" class can accepts only Type of Student Objects for its Methods.

But "MyCustomList<T>" Class can Accept any Type u specifying in "T"

Consider the class Student

/// <span class="code-SummaryComment"><summary>
</span>/// Student Class
/// <span class="code-SummaryComment"></summary>
</span>public class Student
{
    private string fname;
    private string lname;
    private int age;

    /// <span class="code-SummaryComment"><summary>
</span>    /// First Name Of The Student
    /// <span class="code-SummaryComment"></summary>
</span>    public string FirstName
    {
        get { return fname; }
        set { fname = value; }
    }
    /// <span class="code-SummaryComment"><summary>
</span>    /// Last Name Of The Student
    /// <span class="code-SummaryComment"></summary>
</span>    public string LastName
    {
        get { return lname; }
        set { lname = value; }
    }
    /// <span class="code-SummaryComment"><summary>
</span>    /// Age of The Student
    /// <span class="code-SummaryComment"></summary>
</span>    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Creates new Instance Of Student
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><param name="fname">FirstName</param>
</span>    /// <span class="code-SummaryComment"><param name="lname">LastName</param>
</span>    /// <span class="code-SummaryComment"><param name="age">Age</param>
</span>    public Student(string fname, string lname, int age)
    {
        FirstName = fname;
        LastName = lname;
        Age = age;
    }

}

For "StudentList" and "MyCustomList<T>" classes see the code attached.

We can use StudentList like..

        Student dhas = new Student("Manick", "Dhas", 22);
        Student raj = new Student("Sundar", "Raj", 32);

        ///Using a custom strongly typed StudentList
        StudentList mc = new StudentList();
        mc.Add(dhas);
        mc.Add(raj);

        Response.Write("<B><U>Using a custom strongly typed StudentList</B></U><BR>");
        foreach (Student s in mc)
        {
            Response.Write("First Name : " + s.FirstName + "<BR>");
            Response.Write("Last Name : " + s.LastName + "<BR>");
            Response.Write("Age : " + s.Age + "<BR><BR>");
        } 

We can use MyCustomList<T> like..

        ///Creating a list of Student objects using my custom generics
        MyCustomList<Student> student = new MyCustomList<Student>();
        student.Add(dhas);
        student.Add(raj);

        Response.Write("<BR><B><U>Using a list of Student objects using my custom generics</B></U><BR>");
        foreach (Student s in student)
        {
            Response.Write("First Name : " + s.FirstName + "<BR>");
            Response.Write("Last Name : " + s.LastName + "<BR>");
            Response.Write("Age : " + s.Age + "<BR><BR>");
        }
        
        ///Creating a list of Student objects using my custom generics
        MyCustomList<int> intlist = new MyCustomList<int>();
        intlist.Add(1);
        intlist.Add(2);

        Response.Write("<BR><B><U>Using a list of String values using my custom generics</B></U><BR>");
        foreach (int i in intlist)
        {
            Response.Write("Index : " + i.ToString() + "<BR>");
        }

        ///Creating a list of Student objects using my custom generics
        MyCustomList<string> strlist = new MyCustomList<string>();
        strlist.Add("One");
        strlist.Add("Two");

        Response.Write("<BR><B><U>Using a list of int values using my custom generics</B></U><BR>");
        foreach (string str in strlist)
        {
            Response.Write("Index : " + str + "<BR>");
        } 

Output:

Using a custom strongly typed StudentList
First Name : Manick
Last Name : Dhas
Age : 22

First Name : Sundar
Last Name : Raj
Age : 32


Using a list of Student objects using my custom generics
First Name : Manick
Last Name : Dhas
Age : 22

First Name : Sundar
Last Name : Raj
Age : 32


Using a list of String values using my custom generics
Index : 1
Index : 2

Using a list of int values using my custom generics
Index : One
Index : Two

Generics aim to promote:

Binary code reuse,

Performance,

Ease of reading,

Type safety.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ra...aj
Software Developer (Senior) EMD SYSTEMS
India India
Chennai

Comments and Discussions

 
GeneralMy vote of 5 Pin
Prasanta_Prince25-Apr-12 22:25
memberPrasanta_Prince25-Apr-12 22:25 
GeneralMy vote of 3 Pin
rahul_dadge5-Jul-10 22:44
memberrahul_dadge5-Jul-10 22:44 
GeneralGreat Pin
white16528-Jun-09 22:09
memberwhite16528-Jun-09 22:09 
GeneralEasy 2 Learn Pin
Milton_win20-May-09 19:40
memberMilton_win20-May-09 19:40 
GeneralGreat Article Pin
vbUser200614-Oct-07 2:10
membervbUser200614-Oct-07 2:10 
GeneralBrilliant! Pin
Dimitrios Kalemis11-Oct-07 8:42
memberDimitrios Kalemis11-Oct-07 8:42 
GeneralRe: Brilliant! Pin
Ra...aj11-Oct-07 18:28
memberRa...aj11-Oct-07 18:28 
GeneralI have a dream... Pin
Mitri9-Oct-07 9:32
memberMitri9-Oct-07 9:32 
AnswerRe: I have a dream... Pin
rastadiva9-Oct-07 21:54
memberrastadiva9-Oct-07 21:54 
GeneralRe: I have a dream... Pin
Magnus Salgo14-Oct-07 23:15
memberMagnus Salgo14-Oct-07 23:15 
GeneralRe: I have a dream... Pin
Earl Suminda15-Oct-07 13:47
memberEarl Suminda15-Oct-07 13:47 
GeneralRe: I have a dream... Pin
BabuChellathurai16-Oct-07 18:38
memberBabuChellathurai16-Oct-07 18:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 12 Oct 2007
Article Copyright 2007 by Ra...aj
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid