views:

43

answers:

1

For example I have a class named Temp then I assigned values to it using an IList<Temp>. After populating the IList<Temp> I created a dictionary and assign an int key to each object. My question is that how do I remove from IList and dictionary the temp_value with the name b and a with a key value of "2"?

class Temp
{
    public string name { get; set; }

    public Temp(string n)
    {
        this.name = n;
    }
}

Main

class Program
{
    static void Main(string[] args)
    {
        IList<Temp> temp_value = new List<Temp>();

        temp_value.Add(new Temp("a")
        {
            name = "a",
        });

        temp_value.Add(new Temp("b")
        {
            name = "b",
        });

        temp_value.Add(new Temp("b")
        {
            name = "b",
        });

        Dictionary<int, Temp> dic = new Dictionary<int, Temp>();

        for (int i = 0; i < temp_value.Count; i++)
        {
            dic.Add(i, temp_value[i]);                
        }
    }
}
A: 

Dict.remove(2)

This is the simplest way

Since you added the items in dictionary at same index as list.

So if you use dict.remove(2)than you can use IList.remove at(2)

saurabh
Thanks! Forgot the `RemoveAt()`
Rye