C # extension method imaginative refinement Advanced Chapter One: Improved ScottGu "in" extension

Source: Internet
Author: User
Tags bool

First look at the extension of ScottGu in:

Call Example 1:

Call Example 2:

Original address: New "Orcas" Language feature:extension Methods (http://weblogs.asp.net/scottgu/archive/2007/03/13/ new-orcas-language-feature-extension-methods.aspx)

Many of the extension methods are also used as examples of "in", but few people think about it any further. Personally feel this in expansion is not thorough enough to look at 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扩展
public static bool In(this object o, IEnumerable c)
{
 foreach (object i in c)
 {
  if (i.Equals(o)) return true;
 }
 return false;
}

To declare an array (or set) every time you use in, it's a bit cumbersome, and it should be simpler to call like this:

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);
 bool b3 = "Tom".In("Bob", "Kitty", "Tom");
 bool b4 = "Tom".In("Bob", "Kitty", "Tom", "Jim");
}

How does it feel, is it a bit like in SQL?

Related Article

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.