Generic Overview
C # generics are called type parameters. Generic key points:
- In the definition method, the value of a variable is usually used as a parameter, but the type of the variable itself can also be used as a parameter.
- The generic type allows you to specify the type parameter when calling it.
- In. net, generics bring us three obvious advantages: type security,CodeReuse, reduce packing, and unpack operations.
- You can define a type security class through the generic type without compromising the type security, performance, or work efficiency.
- It can be declared and used in any type.
- Use parentheses to enclose general type parameters.
Several examples of the benefits of generics:
Public Class Generic { // Non-generic: insecure type-errors cannot be found during compilation // Execution prompt: system. invalidcastexception: the specified conversion is invalid. Public Void Showvalues1 () {arraylist list = New Arraylist (); list. Add ( 44 ); List. Add ( " String " ); // Compiled Foreach ( Int I In List) {console. writeline ( " Output value: " + I );}} // Generic: type security-Type Mismatch found during compilation Public Void Showvalues2 () {list < String > List = New List < String >(); List. Add ( 44 ); // -- Compilation Error List. Add ( " String " ); Foreach ( String S In List) {console. writeline ( " Output value: " + S );}} // Non-generic: explicit type conversion is required, resulting in performance loss. Public Void Showvalues3 () {arraylist list = New Arraylist (); list. Add ( 44 ); Int I = ( Int ) List [ 0 ]; Console. writeline ( " Output value: " + I );} // Generic: the type is determined during use, and no type conversion is required. Public Void Showvalues4 () {list < Int > List = New List < Int > (); List. Add ( 44 ); Int I = list [ 0 ]; Console. writeline ( " Output value: " + I );}}
Custom generic and call. We can see the role of generics in code reuse.
// Customize a generic class Public Class Mygeneric <t> { Public Void Showvalues (t a) {console. writeline ( " Output value: " + A. tostring ());}} // Use different types of parameters to call custom generics Public Class Test_mygeneric { Public Void Test_int_showvalues () {mygeneric <Int > Fanxing = New Mygeneric < Int > (); Fanxing. showvalues ( 100 );} Public Void Test_string_showvalues () {mygeneric < String > Fanxing = New Mygeneric < String >(); Fanxing. showvalues ( " Hello " );} Public Void Test_doube_showvalues () {mygeneric < Double > Fanxing = New Mygeneric < Double > (); Fanxing. showvalues ( 3.14 );} // Define a generic method: Exchange two variables Public Void Swap <t> ( Ref T, Ref T B) {T temp; temp = A; = B; B = Temp ;} // Exchange integer Public Void Testswap1 (){ Int T = 1 ; Int G = 2 ; Swap ( Ref T, Ref G); console. writeline ( " Output value T: " +T. tostring (); console. writeline ( " Output Value G: " + G. tostring ());} // Exchange string Public Void Testswap2 (){ String T = " Str1 " ; String G = " Str2 " ; Swap ( Ref T, Ref G); console. writeline ( " Output value T: " + T. tostring (); console. writeline ( " Output Value G: " +G. tostring ());} // Exchange object Public Class Usrentity { Public String Username { Get ; Set ;} Public String Sex { Get ; Set ;}} Public Void Testswap3 () {usrentity t = New Usrentity () {username = " James " , Sex = " Male " }; Usrentity g = New Usrentity () {username = " Xiaohong " , Sex = " Female " }; Swap ( Ref T, Ref G); console. writeline ( " Output value T: " + T. username + " : " +T. Sex); console. writeline ( " Output Value G: " + G. username + " : " + G. Sex );}}
Note: This article is based on the relevant training materials.