To find a value in the list, you can use the loop traversal comparison to find the result. The find method is provided in C # and can be directly used as long as the search condition is passed in. As follows:
Public class recordinfo
{
Private string recordid = string. empty;
Private string Itemid = string. empty;
Private string recordanditemid = string. empty;
Private string value = string. empty;
Public String recordid
{
Get
{
Return this. recordid;
}
}
Public String Itemid
{
Get
{
Return this. Itemid;
}
}
Public String recordanditemid
{
Get
{
Return this. recordanditemid;
}
}
Public String Value
{
Get
{
Return this. value;
}
}
Public recordinfo (string tmprecordid, string tmpitemid, string tmprecordanditemid, string tmpvalue)
{
This. recordid = tmprecordid;
This. Itemid = tmpitemid;
This. recordanditemid = tmprecordanditemid;
This. value = tmpvalue;
}
}
- Class Program
- {
- Public static void main (string [] ARGs)
- {
- List <user> usercollection = new list <user> ();
- Usercollection. Add (new user (1, "testone "));
- Usercollection. Add (new user (2, "testtwo "));
- Usercollection. Add (new user (3, "testthree "));
- User resultuser = usercollection. Find (
- Delegate (User user)
- {
- // Return user. userid = 0;
- Return user. userid = 1 & User. username. Equals ("testone ");
- });
- Console. writeline (resultuser! = NULL?
- Resultuser. userid + system. environment. newline + resultuser. Username: "Not Found ");
- Console. Readline ();
- }
- }
- Public class user
- {
- Private int userid = 0;
- Private string username = string. empty;
- Public int userid
- {
- Get
- {
- Return this. userid;
- }
- }
- Public String Username
- {
- Get
- {
- Return this. Username;
- }
- }
- Public user (INT userid, string username)
- {
- This. userid = userid;
- This. Username = username;
- }
- }