There are also many restrictions in the description of a generic class. For example, when defining a variable in a class that requires initialization, you cannot determine whether to use null or 0.
Because it cannot be determined whether it is a value type or a reference type, you can use the default Statement (which is described below ).
The following is the detailed code and necessary explanations of a Document Manager.
1 // define an interface with content and title attributes 2 3 namespace function 4 {5 public interface idocument 6 {7 string content {set; get;} 8 String title {set; get;} 9} 10} 11 // The following is a specific description of the document type, which inherits the above interface 12 namespace function 13 {14 public class document: idocument 15 {16 public document () 17 {18 19} 20 public document (String title, string content) 21 {22 This. title = title; 23 This. content = content; 24} 25 public stri Ng Title 26 {27 get; 28 set; 29} 30 Public String content 31 {32 get; 33 set; 34} 35} 36} 37 38 // The following is a detailed description of the Document Manager class 39 namespace function 40 {41 // The WHERE clause in the following class is required 42 // This generic the specific type of class T must inherit the idcoument interface. 43 // If the T type does not inherit the idcoument interface, therefore, you cannot apply the documentmanager to this generic class 44 // here <t>. You can implement any type that inherits the idcoument of the interface. 45 // The WHERE clause has many other meanings, you can see the final picture to introduce 46 public class documentmanager <t> where T: idocument 47 {48 private readonly queue <t> documentqueue = new queue <t> (); 49 public void adddocument (t doc) 50 {51 // The common point of the lock statement means that when a function calls this module, other functions cannot call 52 again. // wait until the execution is completed and the other functions use 53 lock (this) 54 {55 documentqueue. enqueue (DOC); 56} 57} 58 public bool isdocumentavailable () 59 {60 return (documentqueue. count> 0); 61} 62 public t getdocument () 63 {64 t Doc = default (t); // define the doc variable, and assign the initial value 65 // but the T type here is not clear as the reference type or value type, 66 // when the initial value is assigned, null or 0 67 cannot be specified. // default (t) can be automatically assigned 68 lock (this) 69 {70 Doc = documentqueue according to the type. dequeue (); 71} 72 return Doc; 73} 74 public void displayalldocuments () 75 {76 foreach (T doc in documentqueue) 77 {78 console. writeline (Doc. title); 79} 80} 81 82} 83} 84 // The following is the main function 85 using system; 86 using system. collections. generic; 87 using system. LINQ; 88 using system. text; 89 using system. threading. tasks; 90 91 namespace function 92 {93 class program 94 {95 static void main (string [] ARGs) 96 {97 // instantiate the document manager class and assign values, then, obtain the 98 var dm = new documentmanager <document> (); 99 DM. adddocument (new document ("name1", "content1"); 100 DM. adddocument (new document ("name2", "content2"); 101 DM. adddocument (new document ("name3", "content3"); 102 DM. displayalldocuments (); 103 While (DM. isdocumentavailable () 104 {105 document DOC = NULL; 106 Doc = DM. getdocument (); 107 console. writeline (Doc. content + "" + Doc. title); 108} 109 console. readkey (); 110 return; 111} 112 113} 114}
For more information about the WHERE clause, see http://msdn.microsoft.com/zh-cn/library/bb384067.aspx. Have a detailed and professional introduction.
Constraint statement |
Description |
Where T: struct |
For structure constraints, type T must be a value type |
Where T: Class |
Class constraints specified type T must be a reference type |
Where T: ifoo |
The specified type T must implement the interface ifoo |
Where T: foo |
The specified type T must implement the base class foo |
Where T: New () |
This is a constructor constraint. The specified type T must have a default constructor. |
Where t1: t2 |
This constraint can also be specified. Type T1 is derived from generic Type T2 and is also called a bare type constraint. |
The where clause can only define the constructor constraints for the default constructor, and cannot define the constructor constraints for other constructor.
You can also combine multiple constraints using generic classes. For example, where T: ifoo, new (). The meaning is that T must inherit from the implementation of the ifoo interface and also have the default constructor.
Only the base class, interface, and default constructor can be defined in the where statement.
C # generic functions and limitations of learning