Introduction to generic parameters
Generic Type parameters are commonly used when defining generic types and generic methods. generic type parameters are placeholders of the specified types when instantiating generic methods. Put the generic type parameters in "<>.
Suggestions for naming generic parameters:
(1) When the generic type parameter is a single letter, T is recommended.
(2) When a generic type parameter is defined by a word, it is recommended that T be added before the word.
Copy codeThe Code is as follows: private void PromptName <T> (T t ){}
Private void PromptName <Tuser> (Tuser user ){}
Generic Type parameter Constraints
When defining generic classes, you can apply restrictions on the types used for type parameters when instantiating generic classes. If you use a type that is not allowed by a certain constraint to instantiate a generic class, a compilation error will occur.
Generic constraints:
Constraints |
Description |
T: Structure |
The type parameter must be a value type. You can specify any value type except Nullable. |
T: Class |
The type parameter must be of the reference type. This applies to any class, interface, delegate, or array type. |
T: new () |
The type parameter must have a public constructor without parameters. When used together with other constraints,New ()The constraint must be specified at the end. |
T: <Base Class Name> |
The type parameter must be a specified base class or derived from the specified base class. |
T: <Interface Name> |
The type parameter must be a specified interface or implement the specified interface. Multiple interface constraints can be specified. The constraint interface can also be generic. |
T: U |
The type parameter provided for T must be provided for U or derived from U. |
(1) type parameter constraints are structured (struct ).
Copy codeThe Code is as follows: public class ShowObjectType <T> where T: struct
{
Public void ShowValue <T> (T t)
{
Console. WriteLine (t. GetType ());
}
}
Class GenericConstraint
{
Static void Main ()
{
ShowObjectType <int> showInt = new ShowObjectType <int> ();
ShowInt. ShowValue <int> (5 );
ShowInt. ShowValue (5); // you can export the type of the type parameter from the parameter. The type of the type parameter can be omitted.
// Because the constraint is value type, the following code cannot be compiled
ShowObjectType <string> showString = new ShowObjectType <string> ();
ShowString. ShowValue ("5 ");
Console. Read ();
}
}
(2) The type parameter constraint is class ).
When applying the where T: class constraint, avoid using = and! For type parameters! = Operator, because these operators only test the type as the reference type, and do not test the value equality.Copy codeThe Code is as follows: class GenericConstraint
{
Static void Main ()
{
List <string> list = new List <string> ();
AddClass <string> (list, "hello generic ");
Console. Read ();
}
Private static void AddClass <T> (List <T> list, T t) where T: class
{
List. Add (t );
}
}
(3) type parameter constraints are specific classes.
When the constraint is a specific class, you can use the type parameter to call the attributes and methods of a specific class.Copy codeThe Code is as follows: class GenericConstraint
{
Static void Main ()
{
Person person = new Person {ID = 1, Name = "David "};
PromptName <Person> (person );
Console. Read ();
}
// This constraint T is the Person object or inherits the Person object
Private static void PromptName <T> (T t) where T: Person
{
// The Name attribute of Person can be used here
If (t. Name = "David ")
{
Console. WriteLine ("Person name is David ");
}
String name = t. GetName ();
Console. WriteLine ("Person name is {0}", name );
}
}
Public class Person
{
Private int id;
Public int ID
{
Get {return id ;}
Set {id = value ;}
}
Private string name;
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
Public string GetName ()
{
Return Name;
}
}
(4) restrict multiple parameters.
Copy codeThe Code is as follows: class Base {}
Class Test <T, U>
Where U: struct
Where T: Base, new (){}
(5) The type parameter is not bound.
A type parameter without constraints is called an unbound type parameter.Copy codeThe Code is as follows: class List <T> {}