Control Panel
Forum
Hosting Help
ASP.NET 4 & SQL 2008 R2 Hosting
Resources
Notice
Login
Members
Shared Hosting
|
Upgrade
|
Advertise
|
Tutorials
|
AdSense revenue sharing sites
|
Exam 70-680 Practice Tests
|
Silverlight games
Total
members
:
210367
Average new registrations per day (in last 7 days):
16
New Registration:
Open
Register Now
Home
»
Resources
»
New Concept in .Net Framework 2.0 Generics
ASP.NET
ADO.NET
Web Services
Remoting
Visual Studio 2005
Error Bank
Interview Questions
Tips & Tricks
XML
HTML
Jscript/Javascript
IIS
Windows
General
Submit Resource or code snippet
... and get
surprise gifts
Win Digital camera, ASP.NET Books, Free softwares!!
New Concept in .Net Framework 2.0 Generics
12 Mar, 2008
Author:
Sourabh S Gandhi
Summary
How to Implement Generics in .Net
.NET Classes used :
Let us explore an example to understand the need for Generics and the Implementation part of it. Normally we write the Arraylistclass as:
class MyArrayList
{
private object[] items;
private int count=0;
...
public void Add(object item)
{
items[count++] = item;
}
public object GetItem(int index)
{
return items[index];
}
}
In this code, any untyped object can be added to the list and be read. While adding an untyped object is possible in a direct manner , there has to be an explicit type conversion on a read access. The untyped declaration of the list results in two problems.
The compiler has no chance to verify the content of the list and the necessary type conversions.
Type failures will be recognized only at runtime-or maybe never recognized at all.
You can solve both problems by using typed classes. The base class library provides an abstract base class, CollectionBase in the System.Collections namespace, that will enable you to create typed collections easily. You have to implement the body for the different methods and the indexer. Internally, the objects are stored in an untyped ArrayList, and calls are forwarded to this class. For reference types, this approach works very well, although a new class has to be explicitly developed for each data type. However, collections for value types created in this way are inefficient, because the data needs to be (un)boxed to be stored in the ArrayList internally. The solution for problems like this is the use of generic classes. A blueprint of the class is created just once. Instead of using a particular data type or object, a specific placeholder is added.
// Defining a Generic Class
class MyArrayList
{
private ItemType[] items;
private int count;
...
public void Add(ItemType item)
{
items[count] = item;
}
public ItemType GetItem(int index)
{
return items[index];
}
The class MyArrayList can be specialized for any data-type which would, later in the class, be referenced using the name ItemType class MyArrayList
We replaced the data type of items with ItemType.
Now we can utilize MyArrayList for various types.
For example, the code
MyArrayList iList = new MyArrayList();
Will create an instance of MyArrayList class that accepts and return only integers or that has replaced ItemType in class definition with int. Now we can only add and retrieve integers from iList.
static void Main()
{
MyArrayList iList = new MyArrayList();
iList.Add(25);
int iValue2 = iList.GetItem(1);
}
Similarly for user defined type 'Employee'
static void Main()
{
MyArrayList pList = new MyArrayList();
pList.Add(new Employee("John"));
}
Feedbacks about this page from members:
- No Feedbacks yet !! -
Submit Feedback
View All Feedbacks
Advertise
Privacy Policy
Terms of use
Contact Us
SpiderWorks Technologies Pvt Ltd.
All Rights Reserved.