Returns the first element contained in both, source and candidates. : IEnumerable « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » IEnumerableScreenshots 
Returns the first element contained in both, source and candidates.
 

#region License

/*
 * Copyright  2002-2005 the original author or authors.
 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 *      http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#region Imports

using System;
using System.Collections;
using System.Reflection;

#endregion

namespace Spring.Util
{
    /// <summary>
    /// Miscellaneous collection utility methods.
    /// </summary>
    /// <remarks>
    /// Mainly for internal use within the framework.
    /// </remarks>
    /// <author>Mark Pollack (.NET)</author>
    public sealed class CollectionUtils
    {
        /// <summary>
        /// Returns the first element contained in both, <paramref name="source"/> and <paramref name="candidates"/>.
        /// </summary>
        /// <remarks>The implementation assumes that <paramref name="candidates"/> &lt;&lt;&lt; <paramref name="source"/></remarks>
        /// <param name="source">the source enumerable. may be <c>null</c></param>
        /// <param name="candidates">the list of candidates to match against <paramref name="source"/> elements. may be <c>null</c></param>
        /// <returns>the first element found in both enumerables or <c>null</c></returns>
        public static object FindFirstMatch(IEnumerable source, IEnumerable candidates)
        {
            if (IsEmpty(source|| IsEmpty(candidates))
            {
                return null;
            }

            IList candidateList = candidates as IList;
            if (candidateList == null)
            {
                if (candidates is ICollection)
                {
                    candidateList = new ArrayList((ICollection)candidates);
                }
                else
                {
                    candidateList = new ArrayList();
                    foreach (object el in candidates)
                    {
                        candidateList.Add(el);
                    }
                }
            }

            foreach (object sourceElement in source)
            {
                if (candidateList.Contains(sourceElement))
                {
                    return sourceElement;
                }
            }
            return null;
        }
        /// <summary>
        /// Determines whether the specified collection is null or empty.
        /// </summary>
        /// <param name="enumerable">The collection to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified collection is empty or null; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsEmpty(IEnumerable enumerable)
        {
            if (enumerable == null)
                return true;

            if (enumerable is ICollection)
            {
                return (== ((ICollection)enumerable).Count);
            }

            IEnumerator it = enumerable.GetEnumerator();
            if (!it.MoveNext())
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// Determines whether the specified collection is null or empty.
        /// </summary>
        /// <param name="collection">The collection to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified collection is empty or null; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsEmpty(ICollection collection)
        {
            return (collection == null || collection.Count == 0);
        }

        /// <summary>
        /// Determines whether the specified dictionary is null empty.
        /// </summary>
        /// <param name="dictionary">The dictionary to check.</param>
        /// <returns>
        ///   <c>true</c> if the specified dictionary is empty or null; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsEmpty(IDictionary dictionary)
        {
            return (dictionary == null || dictionary.Count == 0);
        }
   }
}

   
  
Related examples in the same category
1.Determines whether the specified collection is null or empty.
2.Determines whether the collection contains the specified element
3.Determines whether the collection contains all the elements in the specified collection.
4.Aggregate IEnumerable
5.Convert IEnumerable To DataTable
6.Convert IEnumerable by Func
7.Split IEnumerable
8.Shuffle IEnumerable
9.For each IEnumerable
10.Sleep before yield each element in IEnumerable
11.Add WhereIf to IEnumerable
12.Add OrderBy to IEnumerable
13.Add Repeat extension to IEnumerable
14.Yield element in IEnumerable until Predicate
15.IEnumerable extension for get second and third element
16.Simple function to determine if an item is an IEnumerable
17.Object class extension for IEnumerable, IComparable and where
18.Output DataTable and IEnumerable to console
19.Convert IEnumerable to String
20.Convert XmlNodeList to IEnumerable
21.Given a range (start,end) and a number of steps, will yield that a number for each step
22.Compares two sequences.
23.Empty Enumerable/Enumerator
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.