first, let's look at the next general, non-generic simplified linked list class.
Using system; using system. collections; using system. LINQ; using system. text; // do not use system. collections. generic namespace // use system. collections namespace // otherwise, an error will occur when the ienumerable class is inherited./* ----------------------------------------------------------------- * Create a generic instance * Author: herbert * -------------------------------------------------------------------- */namespace generic {// defines a linked list node // contains a node with a value // at the same time Contains the reference public class extends listnode {private object value; Public extends listnode (object Value) {This. value = value;} public object value {get {return value;} private response listnode next; public response listnode next {get {return next;} internal set {next = value ;}} private Keys listnode Prev; public keys listnode Prev {get {return Prev;} internal set {Prev = value ;}}} // Define the Linked List class // The first and last fields of the linked list class and the first and last fields of the record type. Mark the class names list of the head and end of the linked list: ienumerable {private keys listnode first; public writable listnode first {get {return first;} private writable listnode last; Public writable listnode last {get {return last ;}} public writable listnode addlast (Object node) {inclulistnode newnode = new inclulistnode (node); // Add a new element at the end of the linked list. First, create an object of the listnode type. If the linked list is empty, the first and last fields are set to the new element. // otherwise, the new element is added as the last element in the linked list. If (first = NULL) {First = newnode; last = first;} else {last. next = newnode; last = newnode;} return newnode;} public ienumerator getenumerator () {initialize listnode current = first; while (current! = NULL) {yield Return Current. value; current = current. Next ;}}}}
The following is the main method to run.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace generic {class program {static void main (string [] ARGs) {shortlist list1 = new shortlist (); list1.addlast (2); list1.addlast (4 ); // The casting from string to int is invalid // list1.addlast ("6"); foreach (int I in list1) {console. writeline (I);} console. readline ();}}}
The/list1.addlast ("6"); in this example is invalid because. Net does not seem to be able to automatically convert the string to the int type. In that case, the int I in list1 statement will return an error.
The following describes the list type after generics:
Using system; using system. collections; using system. LINQ; using system. text; using system. collections. generic;/* generic * create generic class instance * Author: Herbert * URL: http://www.cnblogs.com/herbert * namespaces */namespace generic {// define a linked list node // contain a node, A node has a value // It also contains the reference public C of the previous node and the next node Lass extends listnode <t> {private T value; Public extends listnode (T value) {This. value = value ;}public T value {get {return value ;}} private writable listnode <t> next; Public writable listnode <t> next {get {return next ;} internal set {next = value ;}} private comment listnode <t> Prev; public comment listnode <t> Prev {get {return Prev ;} internal set {Prev = value ;}}} // define the Linked List class // including the Linked List class and the F type of the lead ticket type The irst and last fields mark the head and end of the linked list public class into list <t>: ienumerable <t> {private writable listnode <t> first; public writable listnode <t> first {get {return first ;}} private writable listnode <t> last; Public writable listnode <t> last {get {return last ;}} public writable listnode <t> addlast (T node) {writable listnode <t> newnode = new writable listnode <t> (node); // The addlast () method adds a new element at the end of the linked list. First, create an object of the listnode type. If the linked list is empty, the first and last fields are set to the new element. // otherwise, the new element is added as the last element in the linked list. If (first = NULL) {First = newnode; last = first;} else {last. next = newnode; last = newnode;} return newnode;} public ienumerator <t> getenumerator () {initialize listnode <t> current = first; while (current! = NULL) {yield Return Current. value; current = current. Next ;}} ienumerator ienumerable. getenumerator () {return getenumerator ();}}}
Main Function
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace generic {class program {static void main (string [] ARGs) {shortlist <int> list1 = new shortlist <int> (); list1.addlast (1 ); list1.addlast (3); list1.addlast (5); foreach (int I in list1) {console. writeline (I);} console. readline ();}}}
Note that ienumberable is applicable to both generic and non-generic types. Add the appropriate namespace when using it. Otherwise, an error will be reported during compilation.
First you have to understand that ienumberable has both generic and not-generic usage. that was my problem. in my code, I have Ded the using system. collections; namespace and in my code I was doing the following:
Ienumerable <test_order> orders = dB. getordersbycustomer (1 );
Where test_order was type produced with a LINQ to SQL class and getordersbycustomer was a stored procedure.
I kept getting the error:
The non-generic type 'System. Collections. ienumerable 'cannot be used with type arguments
Which basically told me my problem, but I was too blind to see it at first. Since I was just callingSystem. Collections. ienumerable, And notSystem. Collections. Generic. ienumerable, I was not allowing the use of generics; hence the error. Simple Add the following to you code and you will not have this error any longer:
using system. Collections. Generic;