Use of the find method in list in C #

Source: Internet
Author: User

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;
}
}

 

  1. Class Program
  2. {
  3. Public static void main (string [] ARGs)
  4. {
  5. List <user> usercollection = new list <user> ();
  6. Usercollection. Add (new user (1, "testone "));
  7. Usercollection. Add (new user (2, "testtwo "));
  8. Usercollection. Add (new user (3, "testthree "));
  9. User resultuser = usercollection. Find (
  10. Delegate (User user)
  11. {
  12. // Return user. userid = 0;
  13. Return user. userid = 1 & User. username. Equals ("testone ");
  14. });
  15. Console. writeline (resultuser! = NULL?
  16. Resultuser. userid + system. environment. newline + resultuser. Username: "Not Found ");
  17. Console. Readline ();
  18. }
  19. }
  20. Public class user
  21. {
  22. Private int userid = 0;
  23. Private string username = string. empty;
  24. Public int userid
  25. {
  26. Get
  27. {
  28. Return this. userid;
  29. }
  30. }
  31. Public String Username
  32. {
  33. Get
  34. {
  35. Return this. Username;
  36. }
  37. }
  38. Public user (INT userid, string username)
  39. {
  40. This. userid = userid;
  41. This. Username = username;
  42. }
  43. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.