How to remove item from List

namespace multikey
{
    class Program
    {
        static void Main(string[] args)
        {
            ForEachClass objForeach = new ForEachClass();
            List<string> tempList = objForeach.BindListItem();
 
            #region ErrorCOde
            ////This code will give you error for removing item from list 
            ////without converting to .ToList()
            //foreach (string item in tempList)
            //{
            //    if (item.Equals("18"))
            //    {
            //        tempList.Remove("18");
            //    }
            //} 
            #endregion
            #region WorkingCodeForRemovingItemFromForEachLoop
            //Easily using this code you can remove item from list
            foreach (string item in tempList.ToList())
            {
                if (item.Equals("18"))
                {
                    tempList.Remove("18");
                }
            } 
            #endregion
 
            foreach (string item in tempList)
            {
                Console.WriteLine(item);
            }
            Dictionary<intList<string>> myDict = new Dictionary<intList<string>>();
            myDict.Add(1, new List<string>() { "i""j" });
            myDict.Add(2, new List<string>() { "a""b","c" });
            myDict.Add(3, new List<string>() { "d","e""f" });
            myDict.Add(4, new List<string>() { "g""h" });
            myDict.Add(5, new List<string>() { "k""l" });
            myDict.Add(6, new List<string>() { "m""n" });
            myDict.Add(7, new List<string>() { "p","r""s" });
            myDict.Add(8, new List<string>() { "t""u","v" });
            myDict.Add(9, new List<string>() { "w""x","y" });
            myDict.Add(0, new List<string>() { "o""q","z" });
            foreach (var item in myDict)
            {
                Console.Write(item.Key +" : \t");
                for (int i = 0; i < item.Value.Count; i++)
                {
                    Console.Write(item.Value[i] +"\t");
                }
                Console.WriteLine();
            }    
       
            Console.Read();
        }
    }
}

Comments

Popular posts from this blog

25ThApril 2020