Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

Previously I've posted a few Helper Classes . This post describes my CollectionsHelper class.

    ///<summary>
    ///</summary>
    ///<example>
    ///<code>
    ///</code>
    ///</example>
    public static class CollectionsHelper
    {
        /*When C# extensions will be available( promised in C# 3, add this keyword to parameter
         * public static bool IsNullOrEmpty(this ICollection c)
        */
 
        ///<summary>
        ///
        ///</summary>
        ///<param name="c"></param>
        ///<returns></returns>
        public static bool IsNullOrEmpty(ICollection c)
        {
           return (c == null || c.Count == 0);
        }
        ///<summary>
        ///
        ///</summary>
        ///<param name="c"></param>
        ///<returns></returns>
        public static bool IsNullOrEmpty(Object[] myArr)
        {
            return (myArr == null || myArr.Length == 0);
        }
  
        //created based on http://www.kirupa.com/net/removingDuplicates2.htm
        //Why it is not in .Net Framework yet? Why HashSet<T> is only in Orcas(http://blogs.msdn.com/bclteam/archive/2006/11/09/introducing-hashset-t-kim-hamilton.aspx)
        public static List<GenericType> removeDuplicates<GenericType>(List<GenericType> inputList)
        {
            Dictionary<GenericType, int> uniqueStore = new Dictionary<GenericType, int>();
            List<GenericType> finalList = new List<GenericType>();
 
            foreach (GenericType currValue in inputList)
            {
                if (!uniqueStore.ContainsKey(currValue))
                {
                    uniqueStore.Add(currValue, 0);
                    finalList.Add(currValue);
                }
            }
            return finalList;
        }
        //Why it is not in .Net Framework yet? Why HashSet<T> is only in Orcas(http://blogs.msdn.com/bclteam/archive/2006/11/09/introducing-hashset-t-kim-hamilton.aspx)
        public static bool AreValuesUnique<GenericType>(List<GenericType> inputList)
        {
            foreach (GenericType currValue in inputList)
            {
                if (inputList.IndexOf(currValue) != inputList.LastIndexOf(currValue))
                    return false;
            }
            return true;
        }
        //Why it is not in .Net Framework yet? Why HashSet<T> is only in Orcas(http://blogs.msdn.com/bclteam/archive/2006/11/09/introducing-hashset-t-kim-hamilton.aspx)
        public static List<GenericType> FindDuplicates<GenericType>(List<GenericType> inputList)
        {
            Dictionary<GenericType, int> uniqueStore = new Dictionary<GenericType, int>();
            List<GenericType> finalList = new List<GenericType>();
 
            foreach (GenericType currValue in inputList)
            {
                if (uniqueStore.ContainsKey(currValue))
                {
                    finalList.Add(currValue);
                }
                else
                {
                    uniqueStore.Add(currValue, 0);
                }
            }
            return finalList;
        }
 
        public static string ToCSVString(List<String> inputList)
        {
            string [] arrStrs=inputList.ToArray();
            return string.Join(",", arrStrs);
        }
        public static string ToCSVString<GenericType>(List<GenericType> inputList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (GenericType currValue in inputList)
            {
                sb.AppendFormat("{0},",currValue.ToString());
            }
            return sb.ToString().TrimEnd(',');
        } 
    }// class CollectionsHelper

Posted on Tuesday, July 17, 2007 12:51 AM | Back to top


Comments on this post: My CollectionsHelper class

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net