Generics where introduced in .NET framework 2.0 in the C# language. Simply said they are collections(lists) which are type safe, i.o.w. you can detect type mismatches at compile time. With other structures (arrays etc) you must check the type in runtime and then type cast to the appropiate type. So generics will make your apps more robust in a way.
Lately I have used the C# language a lot so I learned the power of generics.
Let's take a look at it!
A generic list is declared like this:
Between the brackets you specify the name of the class which will be hold by the list. The compiler expects objects of this type. If you add a different type you will get a compiler error.
Suppose you have a class MyItem which looks like this:
public class MyItemYou can now fill the collection(or should I say List?) FItems like this:
public string Name
FName = value;
MyItem Item = new MyItem();We now have a list with two MyItem objects.
Item.Name = "First";
MyItem Item = new MyItem();
Item.Name = "Second";
You can search within the list using the Find and Findall methods.
Suppose you want the second object you could find it like this:
MyItem SearchedItem = MyItems.Find(delegate(MyItem SItem)Through a anonymous delegate it returns the object which matches the criteria. The Findall method returns a list with objects, for example all objects where a specific attribute matches.
return SItem.Name == "Second";
There is a lot more to explore, for more information look at the MSDN website.
Generics and Delphi
Generics are planned for the Delphi Highlander release, later this year (Roadmaps coming in june!). In september 2006 Nick Hodges showed at the EKon 10 / BorCon Europe conference a preview of the syntax. Bob Swart has published a few notes about it here.