Simple question Related to Generic

D

DNB

I am using generic in the following example.

But I can figure out how to remove an item in the collection??



Thanks

DNB

------------------------------------------------------------------------------------------------

using System.Collections.Generic;

using System.Text;

protected void Page_Load(object sender, EventArgs e)

{

//Generic List Creation

//List is a Generic Class provided by .Net Framework 2.0

//System.Collections.Generics is the Namespace.


List<Person> myPerson = new List<Person>();

myPerson.Add(new Person("1 Test"));

//IF I have to remove "2 Test" how can I do that in the code mentioned
below

myPerson.Add(new Person("2 Test"));

myPerson.Add(new Person("3 Test", 24, "tt"));

myPerson.Add(new Person("4 Test", 24, "tt"));




foreach (Person p in myPerson)

{


Response.Write(p.Name);

Response.Write("&nbsp;");

Response.Write(p.Age);

Response.Write("&nbsp;");

Response.Write(p.Address);

Response.Write("&nbsp;");


Response.Write(p.Company);

Response.Write("<br>");

}


//IF I have to remove "2 Test" how can I do that



}



}



class Person

{

int _Age;


public int Age

{

get { return _Age; }

set { _Age = value; }

}

String _Name;


public String Name

{

get { return _Name; }

set { _Name = value; }

}

String _Address;


public String Address

{

get { return _Address; }

set { _Address = value; }

}

String _Company;


public String Company

{

get { return _Company; }

set { _Company = value; }

}


public Person() { }

public Person(String Name)

{

this.Name = Name;

this.Age = 0;

this.Address = String.Empty;

this.Company = String.Empty;

}



public Person(String Name, int Age, String Address)

{

this.Name = Name;

this.Age = Age;

this.Address = Address;

}






}
 
P

Peter Duniho

I am using generic in the following example.

But I can figure out how to remove an item in the collection??

You'll need to use one of the methods with the word "Remove" in the name.

Your person class doesn't implement IEquatable or override
Object.Equals(), so the only way as the class is now to use Remove() is
if you retain the reference to the object you added, for later use in
removal.

Alternatives include RemoveAll() and RemoveAt(). With the former, you
could use an anonymous method as your predicate:

myPerson.RemoveAll(delegate (Person person)
{ return person.Name == "2 Test"; });

As the name of the method implies, that would remove all instances
matching your criteria. If you only want to remove the first instance
or you know for sure there is only ever one instance, you could search
the list manually:

for (int iperson = 0; iperson < myPerson.Count; iperson++)
{
if (myPerson[iperson].Name == "2 Test")
{
myPerson.RemoveAt(iperson);
break;
}
}

On average that would cut the operation time in half. RemoveAll() will
always enumerate the whole list, so in situations where you can stop at
the first element you find, on average you'd only have to enumerate
half the list instead of all of it.

Of course, you could always implement IEquatable or override
Object.Equals() in your Person class, if appropriate. Then the
Remove() method could be used directly, by creating a new instance of
Person that would test as equal to the one you want to remove and
passing that instance to Remove().

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top