Tuesday 29 April 2014

C# Generics Demo

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;
public partial class GenericsDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
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("Using a custom strongly typed StudentList
");
foreach (Student s in mc)
{
Response.Write("First Name : " + s.FirstName + "
");
Response.Write("Last Name : " + s.LastName + "
");
Response.Write("Age : " + s.Age + "

");
}
///Creating a list of Student objects using my custom generics
MyCustomList student = new MyCustomList();
student.Add(dhas);
student.Add(raj);
Response.Write("
Using a list of Student objects using my custom generics
");
foreach (Student s in student)
{
Response.Write("First Name : " + s.FirstName + "
");
Response.Write("Last Name : " + s.LastName + "
");
Response.Write("Age : " + s.Age + "

");
}
///Creating a list of Student objects using my custom generics
MyCustomList intlist = new MyCustomList();
intlist.Add(1);
intlist.Add(2);
Response.Write("
Using a list of String values using my custom generics
");
foreach (int i in intlist)
{
Response.Write("Index : " + i.ToString() + "
");
}
///Creating a list of Student objects using my custom generics
MyCustomList strlist = new MyCustomList();
strlist.Add("One");
strlist.Add("Two");
Response.Write("
Using a list of int values using my custom generics
");
foreach (string str in strlist)
{
Response.Write("Index : " + str + "
");
}
}
}
///
/// Student Class
///
public class Student
{
private string fname;
private string lname;
private int age;
///
/// First Name Of The Student
///
public string FirstName
{
get { return fname; }
set { fname = value; }
}
///
/// Last Name Of The Student
///
public string LastName
{
get { return lname; }
set { lname = value; }
}
///
/// Age of The Student
///
public int Age
{
get { return age; }
set { age = value; }
}
///
/// Creates new Instance Of Student
///
/// FirstName
/// LastName
/// Age
public Student(string fname, string lname, int age)
{
FirstName = fname;
LastName = lname;
Age = age;
}
}
///
/// Strongly Typed Class
/// Accepts only Student Type
///
public class StudentList : IEnumerable
{
private ArrayList alist = new ArrayList();
///
/// Adds new value of type Student
///
/// Object of Student Class
/// Returns the index of the Object
public int Add(Student value)
{
try
{
return alist.Add(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes The Object from StudentList
///
/// Object of Student Class
public void Remove(Student value)
{
try
{
alist.Remove(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes the student object
///
/// Index of the Student object to be removed
public void RemoveAt(int index)
{
try
{
alist.RemoveAt(index);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Gets the count of the StudentList
///
public int Count
{
get { return alist.Count; }
}
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a StudentList.
///
/// An System.Collections.IEnumerator object that can be used to iterate through the StudentList
public IEnumerator GetEnumerator()
{
try
{
return alist.GetEnumerator();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
///
/// Custom Generic Accepts any Types
///
///
public class MyCustomList : IEnumerable, IEnumerable
{
private ArrayList alist = new ArrayList();
///
/// Adds new value of type T
///
/// Object of Type T
/// Returns the index
public int Add(T value)
{
try
{
return alist.Add(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes The Object from MyCustomList
///
/// Object of Type T
public void Remove(T value)
{
try
{
alist.Remove(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes the object at index
///
/// Index of the T object to be removed
public void RemoveAt(int index)
{
try
{
alist.RemoveAt(index);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Gets the count of the MyCustomList Object
///
public int Count
{
get { return alist.Count; }
}
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a collection.
///
/// An System.Collections.IEnumerator object that can be used to iterate through the collection
public IEnumerator GetEnumerator()
{
try
{
return alist.GetEnumerator();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a collection.
///
/// An System.Collections.IEnumerator object that can be used to iterate through the collection
IEnumerator IEnumerable.GetEnumerator()
{
try
{
return (IEnumerator)alist.GetEnumerator();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}