C # improved Scottgu's "In" Extension

Source: Internet
Author: User

Let's take a look at ScottGu's In extension:

Call Example 1:

Call Example 2:

Original article address: New "Orcas" Language Feature: Extension Methods

Many extension methods use "In" as an example, but few people think about it further. I personally feel that this In extension is not thorough enough. refer to the following code:
  Public static void Example1 ()
{
Bool b1 = 1.In( new int [] {1, 2, 3, 4, 5 });
Bool b2 = "Tom". In (new string [] {"Bob", "Kitty", "Tom "});
}

// ScottGu In extension
Public static bool In (this object o, IEnumerable c)
{
Foreach (object I in c)
{
If (I. Equals (o) return true;
}
Return false;
}


Every time you use In, you need to declare an array (or set), which is a little troublesome. It should be simpler to call it like below:
 

Public static void Example2 ()
{
Bool b1 = 1.In( 1, 2, 3, 4, 5 );
Bool b2 = 1.In( 1, 2, 3, 4, 5, 5, 7, 8 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.