Ejemplo n.º 1
0
        /// <summary>
        /// Removes if collection contains the given element.
        /// <locDE><para />Entfernt das Element falls es in der Liste enthalten ist.</locDE>
        /// </summary>
        /// <typeparam name="T">The element type of the list.<locDE><para />Elementtyp der Liste.</locDE></typeparam>
        /// <param name="collection">The collection.<locDE><para />Die Liste.</locDE></param>
        /// <param name="value">The value.<locDE><para />Der Wert.</locDE></param>
        /// <param name="comparer">The comparer method (or null).<locDE><para />Die Vergleichsmethode (oder null).</locDE></param>
        /// <returns>True if element was contained.<locDE><para />True falls das Element in der Liste war.</locDE></returns>
        public static bool RemoveIfContains <T>(this System.Collections.Generic.ICollection <T> collection, T value,
                                                System.Collections.Generic.IEqualityComparer <T> comparer = null)
        {
            if (null == collection)
            {
                return(false);
            }

            if (IgnoreNullValues && null == value)
            {
                // Don't remove null values
                // Null Wert nicht entfernen
                return(false);
            }

            if (null == comparer)
            {
                if (collection.Contains(value))
                {
                    // Contained, remove
                    // Enthalten, entfernen
                    collection.Remove(value);
                    return(true);
                }
            }
            else
            {
                if (collection.Contains(value, comparer))
                {
                    // Contained, remove
                    // Enthalten, entfernen
                    collection.Remove(value);
                    return(true);
                }
            }

            // Not contained
            // Nicht enthalten
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds if collection not already contains the given element.
        /// <locDE><para />Fügt das Element hinzu, falls es noch nicht in der Liste ist.</locDE>
        /// </summary>
        /// <typeparam name="T">The element type of the list.<locDE><para />Elementtyp der Liste.</locDE></typeparam>
        /// <param name="collection">The collection.<locDE><para />Die Liste.</locDE></param>
        /// <param name="value">The value.<locDE><para />Der Wert.</locDE></param>
        /// <param name="comparer">The comparer method (or null).<locDE><para />Die Vergleichsmethode (oder null).</locDE></param>
        /// <returns>True if element was not contained yet.<locDE><para />True falls das Element noch nicht in der Liste war.</locDE></returns>
        public static bool AddIfNotContains <T>(this System.Collections.Generic.ICollection <T> collection, T value,
                                                System.Collections.Generic.IEqualityComparer <T> comparer = null)
        {
            if (null == collection)
            {
                return(false);
            }

            if (IgnoreNullValues && null == value)
            {
                // Don't add null values
                // Null Wert nicht hinzufügen
                return(false);
            }

            if (null == comparer)
            {
                if (collection.Contains(value))
                {
                    // Already contained
                    // Bereits enthalten
                    return(false);
                }
            }
            else
            {
                if (collection.Contains(value, comparer))
                {
                    // Already contained
                    // Bereits enthalten
                    return(false);
                }
            }

            collection.Add(value);
            return(true);
        }
Ejemplo n.º 3
0
        public bool ContainsKey(K key)
        {
            System.Collections.Generic.ICollection <K> keys = this.Keys;

            return(keys.Contains(key));
        }