The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Hugo2009 (1) [Avatar] Offline
#1
Hello!

In this C# code, why .net throw an exception on line return (MyList)this.OrderBy(p => p.Name).ToList(); ?



class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}

class MyList : List<Pet>
{

public MyList SortByName()
{
return (MyList)this.OrderBy(p => p.Name).ToList();
}
}


Thans
fabrice.marguerie (224) [Avatar] Offline
#2
Re: OrderBy
Here, ToList() returns a List<Pet>, not a MyList. You can't cast the result of ToList() to MyList.