Common collections and generics
1 int[] is a reference type or a value type
2 How to convert between arrays
3 explaining the fundamentals of generics
4 What are the primary and secondary constraints of generics
Common collections and generics
1 int[] is a reference type or a value type
Array types are family types, all of which inherit from System.Array, and System.Array inherit from System.Object. The types of all arrays are reference types.
Memory allocations for arrays of reference types and array of value types:
2 How to convert between arrays
An array type can be implicitly converted if it meets the criteria, including the following: The array dimension must be the same; the target project type and the source project type must have implicit or explicit conversion relationships; The original array item type is not a value type.
The array type can be transformed by the Array.convertall method, which requires the user to provide a conversion algorithm that is passed into the Array.convertall method in the form of a delegate or method.
//Compilation succeededstring[] Sz = {"a","a","a"};Object[] oz =sz;//compilation failed, array of value type items cannot be convertedint[] Sz = {1,2,3};Object[] oz =sz;//compilation failed with a different number of dimensionsstring[,] Sz = {{"a","b"},{"a","C"};Object[] oz = SZ;
Example of array content conversion:
classArrayconvert {Static voidMain (string[] args) {string[] times={"2018-1-1", "2018-1-2", "2018-1-3"}; //use different methods to convertdatetime[] Result1 =OneByOne (times); Datetime[] Result2=ConvertAll (times); //The result is the same foreach(DateTime Iteminchresult1) Console.WriteLine (item. ToString ("YYYY-MM-DD")); foreach(DateTime item2inchresult2) Console.WriteLine (item2. ToString ("YYYY-MM-DD")); Console.read (); } //manually convert individually Private Staticdatetime[] OneByOne (string[] times) {List<DateTime> result =NewList<datetime>(); foreach(String IteminchTimes ) {result. ADD (DateTime.Parse (item)); } returnresult. ToArray (); } //using the Array.conertall method Private Staticdatetime[] ConvertAll (string[] times) {returnArray.convertall (Times,NewConverter<string, datetime>(arrayconvert.datetimetostring)); } Private StaticDateTime datetimetostring (String time) {returnDateTime.Parse (time); } }
Output:
2018-01-01
2018-01-02
2018-01-03
2018-01-01
2018-01-02
2018-01-03
3 explaining the fundamentals of generics
Generics allow programmers to define more general types and algorithms, and to generate specific enclosing types when they are used in concrete form. The type of all generation generic parameters is an open type, it cannot be instantiated, but has all the other attributes of the enclosing type, essentially, it does not differ from the enclosing class. In. NET, another important role of generics is to avoid packing unboxing in container operations.
Generic Example:
classMainClass {Static voidMain (string[] args) { //from open type to enclosing typegenericclass<string> GC =NewGenericclass<string> ("I am a generic type"); Console.WriteLine (GC); Console.read (); } } //a simple generic class Public classGenericclass<t>{T my; PublicGenericclass (T t) {my=T; } Public Override stringToString () {returnMy. ToString (); } }
Microsoft recommends that all generic parameter names start with T, as if the interface is the same as I, which is a generic specification for encoding.
4 What are the primary and secondary constraints of generics
Each generic parameter can have at most one primary constraint, and the primary constraint for generics is that the specified generic parameter must be or inherit from a reference type, with two special primary constraints: class and struct, representing the generic parameter reference type and the value type, respectively.
Each generic parameter can have countless secondary constraints, and the syntax for the secondary and primary constraints is basically the same, but it specifies that a generic parameter must implement the interface specified by all secondary constraints.
Examples of generic primary constraints:
Public classClasst1<t>wheret:exception {PrivateT myexception; PublicClassT1 (T t) {myexception=T; } Public Override stringToString () {//The main constraint guarantees that myexception owns the source member returnMyexception.source; } } Public classClasst2<t>whereR |class { PrivateT MyT; Public voidClear () {//T is a reference type and can be set to nullMyT =NULL; } } Public classClasst3<t>whereR |struct { PrivateT MyT; Public Override stringToString () {//T is a value type and NullReferenceException exception does not occur returnmyt.tostring (); } }
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
. NET Foundation (09) common collections and generics