• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjA4

Related Code
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
StringFormat Extension method for HtmlHelper ASP.NET MVC
Parsing string to enum, extension method

IEnumerable<T> Exist extension method

Exists is an extension method added to IEnumerable<T> to see if an item exists based on a Predicate

0
1.02k 0 0 0 1 6

When working with data structures like List or Collections etc, it may not be so advisable to expose those interface public to other users of the code. Wrote about it on my blog for a while ago. I prefer to expose IEnumerable instead of a List, IList etc when only items should be listed. Sometimes it can be interesting to find if an item exists in the returned list. Instead of adding a ForEach and search for an item (will eventually results in duplication of code), a Exists extension method could be added:

 public static bool Exists<T>(this IEnumerable<T> value, Predicate<T> predicate)
 {
   foreach (var v in value)
   {
      if (predicate(v))
          return true;
   }

   return false;
 }

It can be used in the following way (Where Fees is a property returning IEnumerable<Fee>:

Fees.Exists(fee => fee is NotificationFee)

Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

extension-method

Mark 'extension-method' tag as 'like'

Mark 'extension-method' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 20, 2010 9:13 AM

Fork

 IEnumerable<T> Exist extension method -  @fredrikn Tuesday 20, 2010 9:13 AM


1 Feedback

What's the difference between this and .Any()? -  @jeffhandley Tuesday 20, 2010 9:24 AM

You must log in before you can give any feedback


6 Discussion(s)

Newest Oldest
0

Fredrik, what's the difference between this method and Enumerable.Any?

link | flag  | Reply

 @luisabreu
248
Tuesday 20, 2010 10:30 AM

0

Reply to: luisabreu

Nothing, it seems..

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Tuesday 20, 2010 10:33 AM

0

@jeffhandley:

Good point!

The use of System.Linq will be needed and adds a lot of extension method into the context, methods others maybe shouldn't have access to at all in the current context. Everything to prevent a dev for using members they shouldn't use in the current context to protect the system from potential fails.

But you are right.. it may be duplicated when Any() exists in the current context.

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Tuesday 20, 2010 9:34 AM

-1

Reply to: FredrikN

Well maybe we should reimplementing all methods in System.Linq with that argument?

link | flag  | Reply

 @NPehrsson
40
Monday 26, 2010 2:33 PM

0

Reply to: NPehrsson

Or maybe re-implement you comment ;) hehe...

NP if you want a YAGNI friendly system (prefer) than System.Linq will give you to much methods that you don't really need, why expose hundreds of methods for others (coders in the future or in your team) and let them have the chance to add lot's of work around because of laziness? this is happens to often :(

I like the idea with the existing extension, not using it my self but the Any is no nice word for a methods that shall explain the user story eg: Check if a user exist. The nice thing med DDD is that you always translate you stories to some readable code with no more functions/methods needed to the context.

For me the word Any is more like functional oriented name and it execute a condition (method) ... Exist would be a better word in a DDDish context or in OOP ... This is my opinion... the Any is to abstract it can by anything. if you implement your own Any methods it will be like invent the wheel again, I'm aware of that, but it can also interfere negative not doing it in a specific context. That's why experts often use the word "It depends" because it really depends. For whom, for what, why? ,when? etc...

As I often tell people on my seminar regarding creativity. If you are the one following others rules, than you are always one step behind or more.

link | flag  | Reply

 @johannormen "Code Contributor"
1.24k
Monday 26, 2010 9:12 PM

0

Reply to: NPehrsson

You're welcome to do that if you think that will give you any value.

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Monday 26, 2010 6:10 PM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010