How to delete an item from a generic list

后端 未结 9 461
死守一世寂寞
死守一世寂寞 2020-12-31 05:51

I have a generic list

How do I remove an item?

EX:

Class Student
{
    private number;
    public Number
    {
        get( return number;)
          


        
相关标签:
9条回答
  • 2020-12-31 06:45

    I made a program that contains 7 cards, then shuffle and I hope to take in order to help them

    class Program
    {
    
        static void Main(string[] args)
        {
            Random random = new Random(); 
            var cards = new List<string>();
            //CARDS VECRTOR
            String[] listas = new String[] { "Card 1", "Card 2", "Card 3", "Card 4", "Card 5", "Card 6", "Card 7"};
    
    
            for (int i = 0; i<= cards.Count; i++)
            {
    
                int number = random.Next(0, 7); //Random number 0--->7
    
    
                for (int j = 0; j <=6; j++)
                {
                    if (cards.Contains(listas[number])) // NO REPEAT SHUFFLE
                    {
    
                        number = random.Next(0, 7); //AGAIN RANDOM
    
                    }
                    else
                    {
                        cards.Add(listas[number]); //ADD CARD
                    }
                }
    
            }
    
            Console.WriteLine(" LIST CARDS");
    
            foreach (var card in cards)
            {
                Console.Write(card + " ,");
    
    
            }
    
            Console.WriteLine("Total Cards: "+cards.Count);
    
            //REMOVE
    
            for (int k = 0; k <=6; k++)
            {
               // salmons.RemoveAt(k);
                Console.WriteLine("I take the card: "+cards.ElementAt(k));
                cards.RemoveAt(k); //REMOVE CARD
                cards.Insert(k,"Card Taken"); //REPLACE INDEX
                foreach (var card in cards)
                {
                    Console.Write(card + " " + "\n");
    
                }
    
    
            }
    
    
            Console.Read(); //just pause
    
        }
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-31 06:46

    Well, there is nothing to remove because your list is empty (you also didn't give it an identifier, so your code won't compile). You can use the Remove(T item) or RemoveAt(int index) to remove an object or the object at a specified index respectively (once it actually contains something).

    Contrived code sample:

    void Main(...)
    {
        var list = new List<Student>();
        Student s = new Student(...);
        list.Add(s);
    
        list.Remove(s); //removes 's' if it is in the list based on the result of the .Equals method
    
        list.RemoveAt(0); //removes the item at index 0, use the first example if possible/appropriate
    }
    
    0 讨论(0)
  • 2020-12-31 06:50

    int count=queue.Count;

            while(count>0)
            {
                HttpQueueItem item = queue[0];
                /// If post succeeded..
                if (snd.IsNotExceedsAcceptedLeadsPerDayLimit(item.DataSaleID, item.AcceptedLeadsPerDayLimit) && snd.PostRecord(item.DataSaleDetailID, item.PostString, item.duplicateCheckHours, item.Username, item.Password, item.successRegex))
                {
                    if (item.WaitTime > 0)
                        Thread.Sleep(item.WaitTime);
                    queue.Remove(item);
                }
                    ///If Exceeds Accepted leads per day limit by DataSale..
                else if (!snd.IsNotExceedsAcceptedLeadsPerDayLimit(item.DataSaleID, item.AcceptedLeadsPerDayLimit))
                {
                    queue.RemoveAll(obj => obj.DataSaleID == item.DataSaleID);
                }
                    /// If Post failed..
                else //if (!snd.PostRecord(item.DataSaleDetailID, item.PostString, item.duplicateCheckHours, item.Username, item.Password, item.successRegex))
                {
                    queue.Remove(item);
                }
                count = queue.Count;
            }
    
    0 讨论(0)
提交回复
热议问题