static void Main(string[] args) { List<Users> lol = new List<Users>(); lol.Add(new Users("maks", "kovtun", 234234, "учасник")); lol.Add(new Users("Jeka", "SKRT", 32552, "Π°ΠΊΡ‚ΠΈΠ²ΠΎΠ²Π°Π½ΠΈΠΉ")); } [Serializable] public class Users { public string FirstName; public string LastName; public int ID; public string Status; public string Username; public Users() { } public Users(string fName, string lName, int id, string username ) { this.FirstName = fName; this.LastName = lName; this.ID = id; this.Status = status; this.Username= username; } 
  1. How in the process in the List which contains ID 234234 to add another username?
  2. How in the sheet that contains ID 234234 to change the participant to the active field?

    2 answers 2

     lol.Find(t => t.ID == 234234).Username = "Π΅Ρ‰Π΅ ΡŽΠ·Π΅Ρ€Π½Π΅ΠΉΠΌ"; lol.Find(t => t.ID == 234234).Status = "Π°ΠΊΡ‚ΠΈΠ²Π½Ρ‹ΠΉ"; 

      You get something like a Repository template, a collection in memory.

      Two things are important here:

      1. How will you search for records. In the simplest case, use LINQ, or you can create a set of methods for finding the type FindById, FindByName, and others.
      2. How will you set the values. Your initial values ​​are set through the constructor, and then what? You either make getters / setters, or write separate methods like SetUserName, etc.

      The most commonplace way to modify your example:

       static void Main(string[] args) { List<Users> lol = new List<Users>(); lol.Add(new Users("maks", "kovtun", 234234, "учасник")); lol.Add(new Users("Jeka", "SKRT", 32552, "Π°ΠΊΡ‚ΠΈΠ²ΠΎΠ²Π°Π½ΠΈΠΉ")); lol.First(x => x.ID == 32552).FirstName = "wow"; } [Serializable] public class Users { public string FirstName; public string LastName; public int ID; public string Username; public Users() { } public Users(string fName, string lName, int id, string username) { this.FirstName = fName; this.LastName = lName; this.ID = id; this.Username = username; } } 

      enter image description here

      And further. The example above is just to show direction. Consider how you will handle the situation where you will not have a record with ID = 123 in the repository. In the code above, you will either get an exception in First, or (if FirstOrDefault were there) you would get a null reference if you tried to access FirstName.

      Therefore it is more correct to check if there is such an element:

       var elem = lol.FirstOrDefault(x => x.ID == 32552); if (elem != null) { elem.FirstName = "wow"; } 
      • What is the Dump method? Where did you get it from? - 4per
      • one
        @per I wrote the code in the linqpad, this is his debugging method - a screenshot just from there. Removed, so as not to embarrass. - AK ♦