Thursday 15 May 2014

c# - Can't find the error in this usage of a collection's enumerator -


I want to define a collection with a numerator which can be set at run time.

I defined a calculator:

  public class junior staff employee number: IEnumerable & lt; Employee & gt; {List & lt; Employee & gt; List = new list & amp; Staff; & Gt; (); Public Junior Employee Employees (list & gt; employee & gt; implicit) {list = emplist; } Public IEnumerator & lt; Employee & gt; GetEnumerator () {for (int i = 0; i & lt; list.Qount; i ++) {if (list [i] .ge & lt; 50) {yield returns list [i]; }}} IEnumerator IEnumerable.GetEnumerator () {Return (GetEnumerator ()); }}   

and accepts archive calculator:

  Public class EmployeeCollection: IEnumerable & lt; Employee & gt; {Public List & amp; Staff; Employee & gt; Employee {receive; Set; } Public IEnumerable & lt; Employee & gt; Calculator Type {Received; Set; } Public Employee Compilation () {employee = new list & amp; Staff; & Gt; (); } Public IEnumerator & lt; Employee & gt; GetEnumerator () {Return Enumerator Type. GetEnumerator (); } IEnumerator IEnumerable.GetEnumerator () {Return (GetEnumerator ()); }}   

So, it should be used as the following:

  Fixed zero main (string [] args) {Employee selection Empcoll = new EmployeeCollection (); Empcoll.Employees.Add (new employee ("Fatima", 57)); Empcoll.Employees.Add (new employee ("Evangeline", 52)); Empcoll.Employees.Add (new employee ("Damien", 49)); Empcoll.Employees.Add (new employee ("Cameron", 55)); Empcoll.Employees.Add (new employee ("babu", 24)); Console Write ("Senior Staff \ n"); Empcoll.EnumeratorType = new junior staff employees (empcoll.Employees); Foreign exchange (Employer's Empcoll.GetEnumerator (Employee)) {Console.Write ("+ anEmp.Name +" "+ anEmp.Age +" \ n ");} Console.Readke ();}  < / Pre> 

but I get this compilation error:

The recurrence description type can not work on the variables of 'System.Collections.Generic.IEnumerator' because the 'System Collection.Generic IEnumerator does not include a public definition for 'GetEnumerator'.

How can I fix it?

Loop on IEnumerable For example, do not use IEnumerator.

In this case, instead of

  foreach (employee anEmp) in Empcoll.GetEnumerator () {Console.Write ("+ anEmp. Name + "+ anEmp.Age +" \ N "); }   

You can:

  foreach (Employee anEmp in Empcoll) {Console.Write ("+ anEmp.Name +" "+ anEmp. Age + "\ n");}   

Actually, the novel keyword is just for syntax: using

  (var annuenator = enumerable .net Entertainment ()) {T-loopVar; while (counter. Main (pre)) ({loopVar = enumerator.Current ... ... loop body ...}}   

Said, LINQ operators (such as () () where, could be a better solution to problem solving:

  list & employee; Employee & gt; Emps = ...; Receive (separate junior Impis. Where (E = & gt; I.aj and LT; 50)) {...}    

No comments:

Post a Comment