views:

134

answers:

3

From Jon Skeet's wonderful book C# In Depth, First Edition:

class Film
{
    public string Name { get; set; }
    public int Year { get; set; }

    public override string ToString()
    {
        return string.Format("Name={0}, Year={1}", Name, Year);
    }
}

var films = new List<Film>
{
    new Film {Name="Jaws", Year=1975},
    new Film {Name="Singing in the Rain", Year=1952},
    new Film {Name="Some Like It Hot", Year=1959},
    new Film {Name="The Wizard of Oz", Year=1939},
    new Film {Name="It's a Wonderful Life", Year=1946},
    new Film {Name="American Beauty", Year=1999},
    new Film {Name="High Fidelity", Year=2000},
    new Film {Name="The Usual Suspects", Year=1995}
};

Action<Film> print = film => { Console.WriteLine(film); };
films.ForEach(print);
films.FindAll(film => film.Year < 1960)
.ForEach(print);
films.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));
films.ForEach(print);

A paragraph follows the above-listed snippet of code.

The first half of listing 9.4 involves just setting up the data. I would have used an anonymous type, but it’s relatively tricky to create a generic list from a collection of anonymous type instances. (You can do it by creating a generic method that takes an array and converts it to a list of the same type, then pass an implicitly typed array into that method. An extension method in .NET 3.5 called ToList provides this functionality too, but that would be cheating as we haven’t looked at extension methods yet!)

And the code snippet provided above, is listing 9.4 of the book that the paragraph refers to.

My question: I am trying out the technique outlined in the above paragraph by hand (look at the italicized text) but I can't quite understand what he means.

I tried something like this but it isn't what he meant, I suppose, as it doesn't work (and I didn't expect it to):

using System;
using System.Collections.Generic;

namespace ScratchPad
{

class Film
{
    public string Name { get; set; }
    public int Year { get; set; }

    public override string ToString()
    {
        return string.Format("Name = {0}\tYear = {1}", 
            Name, Year);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ToList<Film>( new[]
        {
            new { Name = "North By Northwest", Year = 1959 },
            new { Name = "The Green Mile", Year = 1999},
            new { Name = "The Pursuit of Happyness", Year = 2006}
        }).ForEach( f => {Console.WriteLine(f);} );

        Console.ReadKey();
    }

    static List<T> ToList<T>(
        System.Collections.IEnumerable list)
    {
        var newList = new List<T>();

        foreach (var thing in list)
            if (thing is T)
                newList.Add((T)thing);

        return newList;

    }
}

}

Note: I know about the IEnumerable.ToList() extension method and have used it many times. I just want to try the technique outlined in the paragraph by hand.

Also, I'm intrigued by scenarios where anonymous types are used outside of Linq, as a syntactic convenience and one of such scenarios is given below. I can always use dynamic in C# 4 and accept an anonymous type as an argument and work with it knowing what I expect. I wish I could do that with C# 3. Something like below:

using System;
using Microsoft.CSharp.RuntimeBinder;

namespace PlayWithAnonType
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintThingy(new { Name = "The Secret", 
Genre = "Documentary", Year = 2006 });
            Console.ReadKey();
        }

    static void PrintWhatever(dynamic whatever)
    {
        // the anonymous type's ToString() will print
        Console.WriteLine(whatever);
    }

    static void PrintThingy(dynamic thingy)
    {
        try
        {
            // I know what the thingy is
            Console.WriteLine("Name = {0}\tGenre = {1}\tYear = {2}",
                thingy.Name, thingy.Genre, thingy.Year);
        }
        catch(RuntimeBinderException ex)
        {
#pragma warning disable 0168
            Console.WriteLine("By thingy, I really meant film. 
Sorry, I should've clarified.");
#pragma warning restore 0168
        }
    }
}

}

Edit They should have a tag named jon-skeet.

+8  A: 

The point was that if we knew about ToList we'd have a way of creating the list without having our own Film type at all. It wasn't that we'd be able to mix an anonymous type with a Film type. In other words, we could do:

// The type of list will be List<T> where T is the anonymous type
var list = new[]
{
    new { Name = "North By Northwest", Year = 1959 },
    new { Name = "The Green Mile", Year = 1999},
    new { Name = "The Pursuit of Happyness", Year = 2006}
}.ToList();

list.ForEach(x => Console.WriteLine("{0} ({1})", x.Name, x.Year));

Glad you're enjoying the first edition, btw - hopefully it won't be too long before the second edition comes out :)

Jon Skeet
Still eagerly awaiting that 2nd edition, of which I regularly receive updates of... postponing. But I'm sure it'll be worth the wait!
Abel
@Abel: Ooh, I didn't know they were sending out postponement notices :( If it's any consolation, I've just received the first two post-first-proofread chapters, which means it really is getting close. I'm as eager to get it out as you are to receive it, I'm sure :)
Jon Skeet
Hi Jon, I do understand what life would be if we were able to use the ToList() extension method. I just want to try out the hack you mentioned, if ToList() did not exist at all.<br />I want some new lines, so let me try out some XHTML.<br />PS: Of course, I am loving the book. This is my second reading. The first one was a quick reading. This one's with trying out every little detail in the book. I just can't wait for the second edition. I'm conducting a 54 hour C# 4 training in my company and distributing copies to pop-quiz winners. :-)
Water Cooler v2
@Water Cooler v2: The hack would basically be reimplementing ToList, that's all :) arootbeer's answer has the appropriate code.
Jon Skeet
I feel like an idiot! :-) I should've simply seen the implementation of ToList() extension in Reflector. Thanks so much, anyways.
Water Cooler v2
+4  A: 

I don't think what Jon is describing is actually very useful to you here. The only point he is making is that he would not normally create an entire class Film just for this example, if it weren't for the problems with creating a List<AnonType>.

Edit: Damn. Alright, this time I am leaving my answer here anyway :)

Rex M
+6  A: 

To actually do it:

public void Main (string[] args)
{
    var films = ToList(new [] {
        new {Name = "Jaws", Year = 1975},
        new {Name = "Singing in the Rain", Year = 1952},
        new {Name = "Some Like It Hot", Year = 1959},
        new {Name = "The Wizard of Oz", Year = 1939},
        new {Name = "It's a Wonderful Life", Year = 1946},
        new {Name = "American Beauty", Year = 1999},
        new {Name = "High Fidelity", Year = 2000},
        new {Name = "The Usual Suspects", Year = 1995}
    }
    );


    films.ForEach(f => Console.Write(f.Name + " - " + f.Year));

}

public List<T> ToList<T> (IEnumerable<T> list)
{
    return new List<T>(list);
}

As others have mentioned, I'm not sure how useful this is. You do get intellisense and all that when you write it, so there's probably some typing savings, at least? :)

arootbeer
Heheh! Copycat. I could do that, too, if I felt like cheating from Reflector. :-)But many thanks.
Water Cooler v2
Tough one! Should the green checky come to you? Tough one. Real tough one. :-)
Water Cooler v2
Glad for my first green checky :P I actually wrote this up based on the question (no Reflector involved); I hadn't really thought about using anonymous types this way. It's a pretty cool shortcut, actually.
arootbeer
I can't believe this is your first green checky. I think I know you. Aren't you that clever C++ chap from Channel 9?
Water Cooler v2
Nope. I'm a C# junkie through and through (so far).
arootbeer