C # basic --- Queue (Queue) application,

Source: Internet
Author: User

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.

 

 


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.