where I use generic collection

Posted by Sekar.C under C# on 3/15/2012 | Points: 10 | Views : 2414 | Status : [Member] | Replies : 6
hi,
what is Generic collection? where I use generic collection in projects in c#
Regard
sekar.c

Regards
Sekar.c



Responses

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

refer this link nice explanation

http://www.dotnetfunda.com/articles/article152.aspx

Thanks

R D Patel

R D Patel

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down
Do you want to know about Arraylist

http://www.functionx.com/csharp1/winprog/collections/arraylist.htm

Use this link



Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi


you can also refer this link

http://www.dotnetfunda.com/interview/exclusive/x2954-what-is-the-drawback-of-arraylist-compared-with-generic-list.aspx

for array list see below link

http://www.dotnetfunda.com/articles/article140.aspx

Thanks

R D Patel

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 3/20/2012 [Member] Bronze | Points: 25

Up
0
Down
Generic Collection: If you want to retrieve a specific type of object, you need to cast that object into its real type. So generic collection solves this problem, by using generic collection, you no need to cast to its real type. generics are type safty.

for example, there is a ArrayList collection to store items of any type in the form of object. (As in .net everything can be represent as object)
But if you want to store a collection of integer, then without using generic you could do this job like:

ArrayLisy arrInt=new ArrayList();
arrInt.Add(1);
arrInt.Add(2);
arrInt.Add(3);

foreach(Object obj in arrInt)
{
int number=(int)obj; //need to type casting.
}

to avoid this type casting use generic collection:

public class MyList<T> : ICollection, IEnumerable
{
private ArrayList _innerList = new ArrayList();
public void Add(T val)
{
_innerList.Add(val);
}
public T this[int index]
{
get
{
return (T)_innerList[index];
}
}

MyList<int> myIntList = new MyList<int>();
myIntList.Add(1); //you can add only integers in myIntList

If you want to add string here, will get compiler error

MyList<String> myStringList = new MyList<String>();
myStringList.Add("1");

If you want to add int here, will get compiler error


Thanks,
Rajni Shekhar

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/20/2012 [Member] Silver | Points: 25
Posted by: Sabarimahesh on: 3/20/2012 [Member] Bronze | Points: 25

Up
0
Down
This is the Best Way to Read Clearly About Generics

http://msdn.microsoft.com/en-us/library/512aeb7t(v=vs.80).aspx

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response