Friday, May 28, 2004

C# Tip: Return a Strongly-Typed Array from the ToArray(type) Method

The help of Visual C# doesn't give an example of using the ArrayList's ToArray method. But here's one from MSDN.

Basiclly, if you have a class BinaryString, the following is right

BinaryString[] bs = (BinaryString[])anArrayList.ToArray(typeof(BinaryString));

But the following is wrong:

BinaryString[] bs = (BinaryString[])anArrayList.ToArray();

I think C# 2.0 will make it simple and easy with its feature of generic type.

C#2.0!! C# 2.0!

No comments: