Nice usage of foreach
-
I once saw a programmer write this.
foreach (User user in users) { return user; }
:laugh:What is horrible about giving preferential treatment to the first user? By the time the app has more users, they probably will have introduced a new keyword yielding some attention to more recent customers... :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
If
users
provides only theIEnumerable
interface then you kinda have to do that. We'd need to see more of the code to determine just how horrible it is. -
IEnumerable.FirstOrDefault[^] aside of course.
That's an Extension Method, not a member of IEnumerable, and therefore has no access to the underlying data; it is likely implemented the same way as the original post. X|
-
That's an Extension Method, not a member of IEnumerable, and therefore has no access to the underlying data; it is likely implemented the same way as the original post. X|
-
I once saw a programmer write this.
foreach (User user in users) { return user; }
:laugh:You're sure it wasn't something like this?
foreach (User user in users)
{
yield return user;
}Marc
Available for consulting and full time employment. Contact me. Interacx
-
You're sure it wasn't something like this?
foreach (User user in users)
{
yield return user;
}Marc
Available for consulting and full time employment. Contact me. Interacx
Wow, I didn't know about the yield operator till now. Thanks for bringing this to my attention. :thumbsup: As for whether the code itself had a yield operator, it was so long ago I cannot remember. I'll give the programmer the benefit of the doubt.
-
What is horrible about giving preferential treatment to the first user? By the time the app has more users, they probably will have introduced a new keyword yielding some attention to more recent customers... :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Just as long as it isn't alphabetical...I got tired of being at the end of the line in Elementary :-\
-
You're sure it wasn't something like this?
foreach (User user in users)
{
yield return user;
}Marc
Available for consulting and full time employment. Contact me. Interacx
I think Luc may have been hinting at this - he'd better remember not to make such hints so subtle next time... "they probably will have introduced a new keyword yielding some attention to more recent customers..."
-
I think Luc may have been hinting at this - he'd better remember not to make such hints so subtle next time... "they probably will have introduced a new keyword yielding some attention to more recent customers..."
:thumbsup:
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
I think Luc may have been hinting at this - he'd better remember not to make such hints so subtle next time... "they probably will have introduced a new keyword yielding some attention to more recent customers..."
Party pooper.
-
Party pooper.
:thumbsup::thumbsup:
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
I once saw a programmer write this.
foreach (User user in users) { return user; }
:laugh:I have seen this when the array doesn't contain a indexing property. return users[0]; // Doesn't work I seem to remember this being the default behaviour for non generic Lists in early .net (2?).
-
I have seen this when the array doesn't contain a indexing property. return users[0]; // Doesn't work I seem to remember this being the default behaviour for non generic Lists in early .net (2?).
I've seen that - and I've also done this "kludge" when I literally didn't have time to look it up: e.g. Must get done fast, bigger fish to fry.. this works - move on. Not ideal - but - depends what's going on.. schedule.. time constraints. It's still readable at least. --Jason
Know way to many languages... master of none!
-
You're sure it wasn't something like this?
foreach (User user in users)
{
yield return user;
}Marc
Available for consulting and full time employment. Contact me. Interacx
-
IEnumerable.FirstOrDefault[^] aside of course.
J4amieC wrote:
IEnumerable.FirstOrDefault[^] aside of course.
If, in fact, you are using .NET 3.5, as otherwise this doesn't exist.
-
If
users
provides only theIEnumerable
interface then you kinda have to do that. We'd need to see more of the code to determine just how horrible it is.Ouch, you sure stepped in the salad! That code necessarily returns the first item in enumerating "users". Had it been a yield return things would be different though.
-
Ouch, you sure stepped in the salad! That code necessarily returns the first item in enumerating "users". Had it been a yield return things would be different though.
Ummm... yeah, so? :confused: