Can you explain me code below :
private static List<Post> _Posts;
public static Post GetPost(Guid id)
{
return _Posts.Find(delegate(Post p)
{
return p.Id == id;
});
}
What is the point to find an object in a generic list by that way ? He can simply iterate the list.
How this delegated method called for each element of list ?
NOTE : if this has a common name, can you update my question's title ?
Thanks !
You're quite right he can iterate over the list, you can think of the code in your question as being conceptually the same as the following:
It requires less code to write your snippet and importantly you are now saying what you want to be found and not exactly how to find it:
In C# 3.0 this can be shortened further using what is called a "lambda expression" to:
Using the least amount of readable code to achieve the same goal makes both writers and readers of that code happier.