1. Scenario Assumption
Suppose there is a string of characters as shown below, and the numbers in the string are separated by commas in English. It is required to use the numbers in this string to quickly generate an array of int type, and use the minimum amount of code as much as possible.
string str = "1,2,3,4,5,6,7,8,9";
2. Solution
I believe that when most students get this question, they will generally give the following solutions:
public int[] String2IntArray(string str) { var strArr = (','); int[] numArr = new int[]; for (int i = 0; i < ; i++) { numArr[i] = Convert.ToInt32(strArr[i]); } return numArr; }
The above code can indeed solve the problems in the above scenario.
3. Problem extension
But at this time the requirements changed, and the char-type array was generated.
At this time, some students will say that since we are generating a char type array, then I can change the data type, so I gave the following code:
public char[] String2CharArray(string str) { var strArr = (','); char[] cArr = new char[]; for (int i = 0; i < ; i++) { cArr[i] = (strArr[i]); } return cArr; }
Another part of the students will say that every time you change the output data type, you need to write a corresponding method, which is not very good if you are not universal. Can generics be used to solve this problem? After thinking about it, I gave the following code:
public T[] String2Array<T>(string str) { var tc = (typeof(T)); var strArr = (','); T[] tArr = new T[]; for (int i = 0; i < ; i++) { tArr[i] = (T)(strArr[i], typeof(T)); } return tArr; }
The above generic code solution is remarkable, but the universality is still poor, and the amount of code is still a bit too much.
What if the array object instance is required to be converted directly? The above generic code solution needs to be optimized and improved. Is there a better solution at this time? There is a answer.
4. Static conversion method of array class
The Array class has a static method ConvertAll, which converts an array of one type into an array of another. This method can effectively solve the pain points of the above problems.
Let's take a look at how this method is defined:
public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter)
- This method has no overloaded methods, it is a static method of the class. It can be called directly through the class name without creating an instance.
- TInput: The type of the source array element.
- TOutput: The type of the target array element.
How to encode and implement the above problem using the ConvertAll method? The following is a code example:
var arr = (','); var numArr = <string, int>(arr, z => (z));
or
var arr = (','); var numArr = <string, int>(arr, delegate (string s) { return (s); });
The ConvertAll method uses only two lines of code. Compared with the aforementioned solution, the results are clear and self-evident.
5. Dig into the bottom line
So how does ConvertAll implement data type conversion of array elements? Let's decompile the method and get the following code:
public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter) { if (array == null) { throw new ArgumentNullException("array"); } if (converter == null) { throw new ArgumentNullException("converter"); } TOutput[] array2 = new TOutput[]; for (int i = 0; i < ; i++) { array2[i] = converter(array[i]); } return array2; }
Let's take a look at how Converter is defined:
public delegate TOutput Converter<in TInput, out TOutput>(TInput input);
Know the reason, we don’t have to be afraid of array conversion problems in the future.
At this time, some students may say: "I may also write the same implementation code as the Converter method without knowing the Converter method in advance." I have to say that you are really great. Since the underlying system provides this method for us to use, wouldn't it be a bit redundant if we encode it again (I didn't know in advance that the Converter method exists and its implementation code)? Why not use it directly?
In this article, we have experienced: Asking questions -> Solving problems -> Extending problems -> Asking questions in the bottom line
This way of thinking will help us quickly improve our coding skills, and I also hope that this way of thinking will bring you some inspiration to solve problems.
This is the end of this article about the detailed explanation of C#’s data type conversion method for implementing array elements. For more related C# array elements data type conversion content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!