In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize the data type conversion of array elements in C #". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to realize the data type conversion of array elements" can help you solve the problem.
1. Scenario hypothesis
Suppose you have a string as shown below, and the numbers in the string are separated by commas in the English state. Requires that you quickly generate an array of type int with the numbers in this string, and use as little code as possible.
String str = "1, 2, 3, 4, 5, 6, 7, 8, 9"
I believe that when most students get this question, they will generally give the following solutions:
Public int [] String2IntArray (string str) {var strArr = str.Split (','); int [] numArr = new int [strArr.Length]; for (int I = 0; I
< strArr.Length; i++) { numArr[i] = Convert.ToInt32(strArr[i]); } return numArr;} 上述代码确实能解决上述场景中的问题。 三、问题延伸 可是这时要求变了,改为生成char类型的数组。 这时一部分同学会说,既然让生成char类型数组,那我改下数据类型不就可以了嘛,于是给出如下代码: public char[] String2CharArray(string str){ var strArr = str.Split(','); char[] cArr = new char[strArr.Length]; for (int i = 0; i < strArr.Length; i++) { cArr[i] = Convert.ToChar(strArr[i]); } return cArr;} 另外一部分同学会说,每改一下输出的数据类型就要再写一个相应的方法,这样不通用不是很好。能不能用泛型解决此问题呢?想了一下,给出了以下代码: public T[] String2Array(string str){ var tc = TypeDescriptor.GetConverter(typeof(T)); var strArr = str.Split(','); T[] tArr = new T[strArr.Length]; for (int i = 0; i < strArr.Length; i++) { tArr[i] = (T)tc.ConvertTo(strArr[i], typeof(T)); } return tArr;} 上面泛型代码解决方案可圈可点,可通用性感觉还是较差,代码量还是有点多。 如果这时要求数组对象实例直接进行转换呢?上面泛型代码解决方案又要进行优化改进。这时有没有更好的解决方案呢?答案是有的。 四、数组类的静态转换方法 数组(Array)类有一个静态方法ConvertAll,该方法能将一种类型的数组转换为另一种类型的数组。该方法能有效的解决上述问题的痛点。 我们来看一下这个方法是怎样定义的: public static TOutput[] ConvertAll(TInput[] array, Converter converter) 该方法没有重载方法,是类的静态方法,无需创建实例便可直接通过类名调用。 TInput:源数组元素的类型。 TOutput:目标数组元素的类型。 上述问题用ConvertAll方法该如何编码实现呢?下面给出代码示例: var arr = str.Split(',');var numArr = Array.ConvertAll(arr, z =>Int.Parse (z))
Or
Var arr = str.Split (','); var numArr = Array.ConvertAll (arr, delegate (string s) {return int.Parse (s);})
The ConvertAll method uses only two lines of code, and compared with the previous solution, the result is clear and self-evident.
5. Get to the bottom of the matter
So how does ConvertAll convert the data types of array elements? Let's decompile the method and get the following code:
Public static TOutput [] ConvertAll (TInput [] array, Converter converter) {if (array = = null) {throw new ArgumentNullException ("array");} if (converter = = null) {throw new ArgumentNullException ("converter");} TOutput [] array2 = new TOutput [array.Length]; for (int I = 0; I < array.Length; iTunes +) {array2 [I] = converter (Array [I]) } return array2;}
Let's take another look at how Converter is defined:
Public delegate TOutput Converter (TInput input)
If we know why, we don't have to be afraid of the problem of array conversion in the future.
At this point, some students may say, "without knowing the Converter method and its implementation in advance, I can also write the same implementation code as the Converter method." I have to say that you are really great, since the bottom of the system provides this method for us to use, wouldn't it be a bit superfluous for us to code it again (not knowing that the Converter method exists and the actual code)? Why not use it directly?
This is the end of the introduction on "how to convert the data type of array elements in C #". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.