This blog is a knowledge base...where I clip cool tricks and urls

convert with generics

clipped from nayyeri.net

Who has said we should write a simple code?!!  I used List<T>.ConvertAll() function to get a List<String> from a List<Double> then converted my List<String> to an Array of Strings.

List<T>.ConvertAll() method is a new .NET 2.0 Generic method which gets a System.Converter object and converts each element in a List<T> from type T to the outgoing type.  It seems this method is using a predicate but actually it doesn't do this.

public String Points

{

    get

    {

        return String.Join(" | ",

            (String[])this.myStudent.Points.ConvertAll

            (new Converter<Double, String>(delegate(Double value)

            {

                return Convert.ToString(value);

            }

            )).ToArray());

    }

}

 blog it

No comments: