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