How to convert Arrays

Source: Internet
Author: User

Analyze problems

In programs, type conversion is everywhere, and array types are no exception. For common types, subclass objects can be implicitly converted to parent classes. All array types are inherited from system. array type, which does not have a relationship with each other, but implicit conversion is still allowed. Its Mechanism is as follows:

1. arrays containing value-type items cannot be implicitly converted to any other type.

2. One premise that two array types can be converted to each other is that the two types have the same dimension.

If the preceding conditions are met, the project type of the source array must be implicitly or explicitly converted to the project type of the target array.

The following code illustrates the conversion mechanism:

// Compilation successful

String [] SZ = {"A", "A", ""};

Object [] Oz = SZ;

// Compilation failed. The array of the value type item cannot be converted

Int [] SZ = {1, 2, 3 };

Object [] Oz = SZ;

// Compilation failed, with different dimensions

String [,] SZ = {"A", "B" },{ "A", "B "}};

Object [] Oz = SZ;

Note:

  Implicit conversion of the array type is not a good method. When an object [] is referenced to an array, any legal operations on the array project will affect the performance, an arraytypemismatchexception will be thrown during the running of any operation that does not match the type.

In addition to conversions of types, Programmers sometimes need to convert content. For example, after a series of user interface operations, the system background may get a datetime array, and the current task is to store them in the database, the interface provided by the data access layer is directly subject to the string [] parameter. At this time, the programmer must convert datetime [] From the content to the string [] object. Of course, the practice is to traverse the entire source array, convert each object one by one, and put it into a container of the target array project type, and finally generate the target array, this method is inferior to array in terms of program flexibility and performance. convertall method. The latter provides a simple interface for converting content between arrays. The programmer needs to specify the project type of the source array, the project type of the object array, and the conversion algorithm, this method can efficiently complete the conversion. The following code shows how to use it.

 

Using system; using system. collections. generic; namespace test {class arrayconvert {static void main () {string [] times = {"-1", "-2", "-3 "}; // convert datetime [] result1 = onebyone (times); datetime [] result2 = convertall (times); // The result is the same foreach (VAR item in result1) {console. writeline (item. tostring ("yyyy-mm-dd");} console. write ("\ r \ n"); foreach (VAR item in result2) {console. writeline (item. tostring ("yyyy-mm-dd");} console. read () ;}// manually convert Private Static datetime [] onebyone (string [] times) {list <datetime> result = new list <datetime> (); foreach (VAR item in times) {result. add (datetime. parse (item);} return result. toarray ();} // use array. convertall method Private Static datetime [] convertall (string [] times) {return array. convertall (Times, New converter <string, datetime> (arrayconvert. datetimetostring);} // conversion algorithm Private Static datetime datetimetostring (string time) {return datetime. parse (time );}}}

The code above demonstrates the differences between the two types of content conversion. The two implement the same function, but the array. convertall does not require a program to traverse the array manually or generate a temporary container object. A more prominent advantage is that it can accept a dynamic algorithm as the conversion basis. This algorithm can be passed in as a method or as a delegate. The concept of delegation is described in later sections. This mechanism ensures that array. convertall has high Program Flexibility and running performance.

Answer

The array type can be implicitly converted when the conditions are met, including: the array dimension must be the same; the target project type and source project type must have an implicit or explicit conversion relationship; the project type of the source array is not a value type.

The array type can be converted by using the array. convertall method. You need to provide a conversion algorithm to pass in the array. convertall method as a delegate or method.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.