C # basic --- Queue (Queue) application,
Queue, features FIFO. In some projects, we will encounter Check on some data. If the data does not meet the conditions, the information that fails will be returned to the interface. However, some data may Check many conditions. If a Data fails to pass many conditions, all errors will be returned to the interface, which may make the user helpless. Sometimes we are in a process. Only the first non-conforming error in the Check process is prompted to the user for modification. First, we thought of the queue. The queue registers all the Check Methods and then columns them in sequence. Run.
Demo Background:
XX Company has requirements on the place where employees live, surnames, and ages.
I. First, we define the object Model:
public class Person { public string Name { get; set; } public string Address { get; set; } public int Age { get; set; } }
public class ErrorMessage { public string ErrorCode { get; set; } public string ErrorInfo { get; set; } public override string ToString() { return string.Format("{0}:{1}", ErrorCode, ErrorInfo); } }
II. Implementation Methods:
Note: Queue <Func <Person, ErrorMessage> defines a fun () delegate through Queue, and then adds the CheckName and CheckAge methods. register the methods in the queue respectively, and then run the Check method. Once the Check fails, the system jumps out of the loop.
Public class Program {public static void Main (string [] args) {Person person = new Person () {Name = "Frank Zhang", Address = "Chengdu ", age = 60 }; Queue <Func <Person, ErrorMessage> myQueue = new Queue <Func <Person, ErrorMessage> (); ErrorMessage errorMessage = null; myQueue. enqueue (CheckName); myQueue. enqueue (CheckAge); var count = myQueue. count; for (int index = 0; index <count; index ++) {var CheckMethod = myQueue. Dequeue (); errorMessage = checkMethod (person); if (errorMessage! = Null) {Console. WriteLine (errorMessage. ToString (); break ;}} public static ErrorMessage CheckName (Person person) {if (person! = Null & person. name. endsWith ("Zhang") {return new ErrorMessage () {ErrorCode = "Error_001", ErrorInfo = "We do not recruit Zhang's" };} return null ;} public static ErrorMessage CheckAge (Person person) {if (person! = Null & person. Age> 50) {return new ErrorMessage () {ErrorCode = "Error_002", ErrorInfo = "We do not enroll elders" };} return null ;}}
Iii. Summary
The above are some tips for using Check in the project. It feels good. Share it. I hope you can share some good ideas with us.