Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:03:23+00:00 2026-05-15T18:03:23+00:00

From Jon Skeet’s wonderful book C# In Depth, First Edition : class Film {

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:03:24+00:00Added an answer on May 15, 2026 at 6:03 pm

    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? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took this from Jon Skeet's C# in depth. He mentioned the following is
I've been reading Jon Skeet's C# In Depth: Second Edition and I noticed something
I have pasted some code from Jon Skeet's C# In Depth site: static void
I'm new to LINQ,My knowledge on that library is from Jon Skeet's book C#
I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet
Following from my last question which @Jon Skeet gave me a lot of help
From Jon Skeet's blog: What does the following comment mean? // The line below
I'm reading the book Real-world functional programming by Tomas Petricek and Jon Skeet and
In his book, Jon Skeet refers to 7 restrictions on implicit typing. I need
The below example is got from Jon Skeet's article, Parameter passing in C# .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.