有人可以详细解释下面的代码 [英] can some one explain about the following code in detail
问题描述
string data = DataTextBox.Text;
列表< string> gradeString = new List< string>
(data.Split( new string [] { \\\\ n},StringSplitOptions.RemoveEmptyEntries));
List< int> intList = gradeString.ConvertAll< int>(Convert.ToInt32);
intList.Sort();
string [] text = new string [ 100 ]; < / int > < / int > < / string < span class =code-keyword>> < / string >
首先,它会复制DataTextBox.Text中的文本属性。
string data = DataTextBox.Text
然后使用Split调用返回的数据实例化一个新的List< string> ;.
List< string> gradeString = new 列表< string>(data.Split( new string [] { \\\\ n},StringSplitOptions.RemoveEmptyEntries )); < / string > < / string >
(data.Split(new string [] {\\\\ n},StringSplitOption.RemoveEmptyEntries))
实际搜索\\\\ n的数据,并向左边添加任何数组(除了emtpy字符串,然后跳到下一个\\\\ n。
List< string> ;然后填充数组的内容,因为它是IEnumerable< t>。
所以,要进一步分解它...
<前郎=c#> string test = 看看如何这是有效的!;
// 这就是data.Split部分的工作原理。
string [] split = test.Split( new string [] { \\\\ nn});
// string [0] ==看看如何
// string [1] ==这个有效!
List< string> testSplit = new List< string>(split);
< / string > < / string >
给定通用类型和转换器,下一部分会在列表中进行全部转换。
< int> < / int >是泛型类型,
Convert.ToInt32是转换器。
这基本上说,如果是int类型,我希望将gradeString的内容转换为List。
intList.Sort()按升序对列表进行排序。
string data = DataTextBox.Text;
List<string> gradeString = new List<string>
(data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
List<int> intList = gradeString.ConvertAll<int>(Convert.ToInt32);
intList.Sort();
string[] text = new string[100];</int></int></string></string>
First it makes a copy of the text in the DataTextBox.Text property.
string data = DataTextBox.Text
Then instantiates a new List<string>, with the data returned by the Split call.
List<string> gradeString = new List<string>(data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));</string></string>
The (data.Split(new string[] { "\r\n" }, StringSplitOption.RemoveEmptyEntries))
actuall searches data for "\r\n" and adds anything to the left to an array (except emtpy strings, then skips to the next "\r\n".
The List<string> is then populated with the contents of the array since it is an IEnumerable<t>.
So, to break it down further..
string test = "Look at how\r\nthis works!"; // This it how the data.Split part works. string[] split = test.Split(new string[] { "\r\n" }); // string[0] == "Look at how" // string[1] == "this works!" List<string> testSplit = new List<string>(split); </string></string>
The next portion does a convert all on the list, given the generic type and a converter.
<int></int>is the generic type and
Convert.ToInt32is the converter.
This basically says, i want the contents of gradeString to be converted to a List if int types.
intList.Sort()sorts the list in ascending order.
这篇关于有人可以详细解释下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!