C # Basic---queue (queued) application

Source: Internet
Author: User

Queue queues, features FIFO first. In some projects we will encounter some data check, if the data does not meet the criteria will be the non-passing information back to the interface. However, for some data may check many conditions, if a data once a lot of conditions do not pass, then all errors back to the interface, you may be left stranded. We sometimes tend to be in a process. Only the first non-qualifying error in the check process will be prompted to the user for modification. First we think of the queue, register all the check methods through the queue, and then dequeue them in turn. Perform.

Demo background:

XX company recruit, to the employee's place of residence, surname, age have requirements.

First, we define the entity model:

     Public class person    {        publicstringgetset;}          Public string Get Set ; }          Public int Get Set ; }    }
     Public classerrormessage { Public stringErrorCode {Get;Set; }  Public stringErrorInfo {Get;Set; }  Public Override stringToString () {return string. Format ("{0}:{1}", ErrorCode, errorinfo); }    }

Two. Implementation method class:

Note:queue<func<person,errormessage>> defines a fun () delegate through a Queue, and then adds the CheckName and Checkage methods. The method is registered to the queue, then the pair, execute each check method, once the check does not pass, and then jump out of the loop.

 Public classProgram { Public Static voidMain (string[] args) { person person=NewPerson () {Name="Frank Zhang", Address="Chengdu", Age= -            }; Queue<func<person, errormessage>> myqueue =NewQueue<func<person, errormessage>>(); ErrorMessage errormessage=NULL;            Myqueue.enqueue (CheckName);            Myqueue.enqueue (Checkage); varCount =Myqueue.count;  for(intindex =0; Index < count; index++)            {                varCheckmethod =Myqueue.dequeue (); ErrorMessage=Checkmethod (person); if(ErrorMessage! =NULL) {Console.WriteLine (errormessage.tostring ());  Break; }            }        }         Public Staticerrormessage checkname (person person) {if(Person! =NULL&& person. Name.endswith ("Zhang"))            {                return NewErrorMessage () {ErrorCode ="error_001", errorinfo ="We don't call Zhang's name." }; }            return NULL; }         Public Staticerrormessage checkage (person person) {if(Person! =NULL&& person. Age > -)            {                return NewErrorMessage () {ErrorCode ="error_002", errorinfo ="We don't recruit seniors ." }; }            return NULL; }    }

C # Basic---queue (queued) application

Related Article

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.