tags:

views:

172

answers:

4

The MSDN gives this code example in the article on the Func Generic Delegate:

Func<String, int, bool> predicate = ( str, index) => str.Length == index;

String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
IEnumerable<String> aWords = words.Where(predicate).Select(str => str);

foreach (String word in aWords)
    Console.WriteLine(word);

I understand what all this is doing. What I don't understand is the

Select(str => str)

bit. Surely that's not needed? If you leave it out and just have

IEnumerable<String> aWords = words.Where(predicate);

then you still get an IEnumerable back that contains the same results, and the code prints the same thing.

Am I missing something, or is the example misleading?

+1  A: 

No it's not needed.

Such a construct could be used if you wanted to force a sequence to be lazily evaluated i.e. to prevent casting. If you had a method that returned a List<T> but declared an IEnumerable<T> return type then a client could cast the return type and manipulate the underlying list directly. Obviously this is a very bad idea, but a class could protect its state by applying an identity select such as the one used in this example:

public IEnumerable<T> Items
{
   get { return privateList.Select(i => i); }
}
Lee
In such cases, I would "return privateList.AsReadOnly();" instead, even if returning IEnumerable<T>.ReadOnlyCollection has the advantage that it implements ICollection, which makes some LINQ operators (such as ToArray()) faster because the collection length is known.
Daniel
+8  A: 

The Select is indeed redundant.

I suspect that this example may have been "translated" from the query comprehension syntax, as in:

IEnumerable<String> aWords = 
    from w in words
    where (...)
    select w;

When using this syntax, you have to select at the end, it's just how the compiler works. When using the Where extension method, however, it's completely unnecessary unless you actually need to do a separate projection.

Or, maybe it's just a mistake. The MSDN writers aren't infallible!

Aaronaught
does this mean the lambda syntax is more efficient?
fearofawhackplanet
@fearofawhackplanet: They're identical, just different ways of writing the same query.
Aaronaught
A: 

Indeed, it's not needed, there's a feedback button on bottom of msdn page. Tell them the .Select is not needed. The mere fact that the Select clause is just:

Select(str => str)

It receives string and outputs the same string

Michael Buen
A: 

You have a very strange link there. This topic is not in TOC, and it has the following at the top:

"[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]"

The first line also looks like writer's comment to himself/herself.

And since VS 2010 and .NET 4.0 just released, I guess, this is some kind of broken topic that hasn't been deleted/replaced in time.

I think, the correct URL now for this content is this: http://msdn.microsoft.com/en-us/library/bb534303.aspx

By the way, how did you get your URL? Was it through MSDN search or something else?

Alexandra Rusina