Determine whether a given collection only contains a single unique object : ICollection « Collections Data Structure « C# / C Sharp






Determine whether a given collection only contains a single unique object

  
        
#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>
        /// Determine whether a given collection only contains
        /// a single unique object
        /// </summary>
        /// <param name="coll"></param>
        /// <returns></returns>
        public static bool HasUniqueObject(ICollection coll)
        {
            if (coll.Count == 0)
            {
                return false;
            }
            object candidate = null;
            foreach (object elem in coll)
            {
                if (candidate == null)
                {
                    candidate = elem;
                }
                else if (candidate != elem)
                {
                    return false;
                }
            }
            return true;
        }
   }
}

   
    
  








Related examples in the same category

1.Finds a value of the given type in the given collection.
2.Adds a new element to the specified collection.
3.Adds all of the elements of the "c" collection to the "target" collection.
4.Removes all the elements from the collection.
5.Determines whether the collection contains the specified element.
6.Removes the specified element from the collection.
7.Retains the elements in the target collection that are contained in the specified collection
8.Returns an array containing all the elements of the collection.
9.Converts an ICollection instance to an ArrayList instance.
10.Tests if the specified object is a collection and converts it to its string representation.
11.Determines whether the collection contains the specified element
12.Adds the specified element to the specified collection
13.Determines whether the collection contains all the elements in the specified collection.
14.Removes all the elements from the target collection that are contained in the source collection.
15.Converts an System.Collections.ICollection instance to an System.Collections.ArrayList instance.
16.Copies the elements of the ICollection to a new array of the specified element type.
17.Is a Collection Null Or Empty Or Default
18.Converts the specified collection to its string representation.
19.Group the collection using a function which returns the key.
20.Convert ICollection to T[]
21.Convert ICollection to T[]
22.ConvertAll ICollection to TOut[] with Converter
23.Add range to Collection
24.Lambda Collections Generic Set