Saturday, December 6, 2008

Generic C#

In my last post I introduced Generics and showed some examples in VB.NET. Generics can also be used in C#. The TypedList class from m previous post would look like this in C#:

 class TypedList<itemType><br />{<br /> ArrayList list = new ArrayList();<br /><br /> public void Add(itemType item) {<br /> list.Add(item);<br /> }<br /><br /> public itemType Item(int index) {<br /> return (itemType)list[index];<br /> }<br />}<br />

You would declare a TypeList that accepted dates list this:

 TypedList<System.DateTime> list = new TypedList<System.DateTime>();

No comments: