Understanding of generic constraints and understanding of generic Constraints
1. Reference Type ConstraintsStruct RefSample <T> where T: class reference type uses Class to indicate constraints. Other reference types are specific constraints. It indicates that the constraint must be a class (reference type) and cannot be a value type (int, char, datatime, struct). It can be an interface, and an array is a reference type, because an object needs to be created when an array is defined. Although it is defined as RefSample <T>, the input must be of the reference type, but RefSample is still of the value type.2. Value Type ConstraintsClass ValSample <T> where T: struct is the reference type, because int, char, and Other types are structValSample <int>3. constructor type constraintsPublic T CreateInstance <T> () where T: new () {return new T ();} the specified type T must have a constructor, both CreateInstance <int> and CreateInstance <object> are valid. However, CreateInstance <strings> does not have a constructor.4. Conversion Type ConstraintsOne constraint allows you to specify another type. Type arguments must be implicitly converted to this type through consistency, reference, or boxing conversion. You can also specify that a type real parameter must be converted to another type real parameter -- this is called a type parameter constraint. It can be exchanged, that is, all types that can be converted to the target type through packing or forced conversion can be used for type parameter input. Class Sample <T> where T: Stream valid: Sample <Stream> the compliance constraints are invalid: sample <String> because the String type cannot be forcibly converted to Stream or struct Sample by referencing or packing the box, where T: IDisposable specifies that T must be a reference type of the IDisposable type. valid: sample <SqlConnection> invalid reference conversion: Sample <StringBuilder> analysis: Why does SqlConnection and StringBuilder fail? They are all reference types 1. sqlConnection implements the IDisposable interface, so it can be changed. stringBuilder only implements the ISerializable interface and cannot be converted to IDisposable class Sample through a channel <T> where T: IComparable <T> because IComparable <T> is considered as a constraint, the IComparable <T> Type can be analyzed. isValueType, true is the value type, and false is the reference type typeof (IComparable <T> ). the result of IsValueType is false, indicating that the reference type is valid: Sample <int> (Packing conversion) is invalid: Sample <FileInfo> You can also specify multiple constraints: class sample <T> where T: stream, IEnumerable <string>, IComparable <int> class Sample <T, U> where T: U valid: Sample <Stream, IDisposable> invalid: Sample <string, IDiposable> conclusion: it depends on whether the input class parameters can be converted, check whether the specified parameters and input class parameters implement the same interface. If the interface is implemented, the interface can be used; otherwise, the interface cannot be used. It cannot be the following: System. Object, System. Enum, System. ValueType, System. Delegate, structure or seal class (String)5. Combination ConstraintsThere are multiple constraints on the Type parameter. Note: Only one type can be used. The value type and reference type cannot exist at the same time. A useless type is the reference type and the value type. Since each value type has a non-constructor, no constructor constraints can be created thereafter. Valid: class Sample <T> where T: class, IDisposable, new () class Sample <T, u> where T: Stream where U: IDispsable invalid: class Sample <T> where T: class, struct (no real-time reference type is value type, so it is invalid) class Sample <T> where T: Stream, class (the reference type constraint should be the first constraint, put at the beginning, so it is invalid) Stream only restricts the input parameter to the specific type of Stream, the class constraint is the reference type. At first, I understood the error class Sample <T> where T: new (), Stream (new () must be placed at the end) class Sample <T> where T: IDisposable, S Tream (the class must be placed before the interface, so it is invalid) class Sample <T> where T: XmlReader, IComparable, IComparable (the same interface cannot appear multiple times for conversion type constraints) class Sample <T, U> where T: struct where U: Class, T (type parameter "T" has the "struct" constraint, therefore, "T" cannot be used as the "U" constraint, so it is invalid) class Sample <T, U> where T: Stream, U: the error message returned when the IDisposable syntax shows that this version is also valid on the Internet. I do not understand it: class Sample <T> where T: struct, IDisapsable is the value type? Class Sample <T, U> where T: class where U: struct, T is the reference type. Why are constraints on U with the value type? Hope you can correct